我正在使用ColdBox 3.5和ColdFusion 10 ORM开发博客应用程序,我随机收到以下错误消息:
错误类型:应用程序:[N / A]
错误消息:Hibernate操作中的异常。 更新/删除的行不存在或会话包含过时数据。
根本原因:org.hibernate.StaleObjectStateException:行被另一个事务更新或删除(或未保存的值映射不正确):[Entry#297e1bfa369cf17501369d26ffae00a4]
/model/entry/Entry.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>
请注意标有“5次中有1次”和“每次都有效”的部分。我不知道我做错了什么。我有其他类似的对象,他们使用链接表,我收到类似的错误消息。我查看了SQL日志。准备插入EntryCategory
表时似乎发生错误。有什么想法吗?
答案 0 :(得分:0)
使用HQL并使用(?)中的ID。这非常有效。
如果实体已有数组,请先使用ArrayClear()。
更新:
categories = ormExecuteQuery("from Category where Id IN (:Ids)",
{Ids=listToArray(FORM.categoryIDs)});
entry.setCategories(categories);