我正在使用冷箱3.5和ColdFusion 10 ORM博客應用程序,並且我隨機得到以下錯誤消息:ORM - 鏈接表錯誤
錯誤類型:應用:[N/A]
錯誤消息:Hibernate操作中的異常。 更新/刪除的行不存在或者會話包含陳舊的數據。
根源:org.hibernate.StaleObjectStateException:行被更新或者由另一個事務刪除(或未保存值的映射是不正確的):[輸入#297e1bfa369cf17501369d26ffae00a4]
/模型/進入/條目。 CFC:
component
persistent="true"
table="Entry"
output="false"
{
// primary key
property name="entryID" fieldtype="id" ormtype="string" type="string" length="32" generator="uuid";
// properties
property name="title" fieldtype="column" ormtype="string" type="string" length="100" notnull="true";
property name="alias" fieldtype="column" ormtype="string" type="string" length="100" notnull="true";
property name="body" fieldtype="column" ormtype="string" type="string" sqltype="nvarchar(max)" notnull="true";
property name="allowComments" fieldtype="column" ormtype="boolean" type="boolean" sqltype="bit" dbdefault="1" default="true" notnull="true";
property name="released" fieldtype="column" ormtype="boolean" type="boolean" sqltype="bit" dbdefault="1" default="true" notnull="true";
property name="releasedDate" fieldtype="column" ormtype="timestamp" type="date";
property name="addDate" fieldtype="timestamp" ormtype="timestamp" type="date";
// relations
property name="categories" fieldtype="many-to-many" cfc="model.category.Category" linktable="EntryCategory" fkcolumn="entryID" inversejoincolumn="categoryID" singularname="category" cascade="all" lazy="true";
property name="comments" fieldtype="one-to-many" cfc="model.entryComment.EntryComment" fkcolumn="entryID" singularname="comment" cascade="all-delete-orphan";
property name="user" fieldtype="many-to-one" cfc="model.user.User" fkcolumn="userID" notnull="true" cascade="save-update";
property name="views" fieldtype="one-to-many" cfc="model.entryView.EntryView" fkcolumn="entryID" singularname="view" cascade="all-delete-orphan";
// validation
this.constraints = {
"title" = {"required" = true},
"body" = {"required" = true},
"allowComments" = {"required" = true, type="boolean"},
"released" = {"required" = true, type="boolean"},
"categories" = {size=1},
"user" = {type="component"}
};
}
/model/category/Category.cfc:
component
persistent="true"
table="Category"
schema="system"
output="false"
{
// primary key
property name="categoryID" fieldtype="id" ormtype="string" type="string" length="32" generator="uuid";
// properties
property name="name" fieldtype="column" ormtype="string" type="string" length="50" notnull="true";
property name="alias" fieldtype="column" ormtype="string" type="string" length="50" notnull="true";
property name="description" fieldtype="column" ormtype="string" type="string" default="" length="200";
property name="addDate" fieldtype="timestamp" ormtype="timestamp" type="date";
property name="active" fieldtype="column" ormtype="boolean" type="boolean" sqltype="bit" dbdefault="1" default="true" notnull="true";
// relations
property name="entries" fieldtype="many-to-many" cfc="model.entry.Entry" linktable="EntryCategory" fkcolumn="categoryID" inversejoincolumn="entryID" lazy="true" cascade="all" singularname="entry" inverse="true";
// validation
this.constraints = {
"name" = {"required" = true},
"active" = {"required" = true, type="boolean"}
};
}
下面是我運行代碼:
<cfscript>
entry = entityNew("Entry", {
"title" = "test",
"body" = "test",
"alias" = "test",
"allowComments" = 0,
"released" = 0,
"user" = entityLoadByPK("User", "297e1bfa3697d377013697f53ca10084")
});
// works 1 out of 5 times
entry.setCategories([entityLoadByPK("Category", "297e1bfa36986e69013698c3e54f000d")]);
// works every time
//entry.setCategories(entityLoad("Category", "297e1bfa36986e69013698c3e54f000d"));
//entry.setCategories(entityLoad("Category"));
entitySave(entry);
ormFlush();
</cfscript>
通知標記爲部分「作品1出5倍」和「屢試不爽」。我不明白我做錯了什麼。我有其他類似於這些地方使用鏈接表的對象,我收到類似的錯誤消息。我回顧了SQL日誌。當它準備好插入EntryCategory
表時,該錯誤似乎就會發生。有任何想法嗎?
會'entry.addCategory(entityLoadByPK( 「類別」, 「297e1bfa36986e69013698c3e54f000d」))'更好地工作? – Henry 2012-04-10 17:15:48
不完全。我正在使用的維護頁面設置爲允許用戶選擇多個類別。所以,我想要做的是將ID值轉換爲實體數組,然後執行entry.setCategories(my_category_array) – 2012-04-10 17:22:53
如果是這種情況,請使用HQL並使用(?)中的ID。這很好。如果實體已經有一個數組,首先使用ArrayClear()。 – Henry 2012-04-10 17:36:33