0

我正在使用 ColdBox 3.5 和 ColdFusion 10 ORM 开发博客应用程序,我随机收到以下错误消息:

错误类型:应用程序:[N/A]

错误消息:休眠操作中的异常。更新/删除的行不存在或会话包含陈旧数据。

根本原因: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表时,似乎会发生错误。有任何想法吗?

4

1 回答 1

0

使用 HQL 并在 (?) 中使用 where ID。这很好用。

如果实体已经有一个数组,请先使用 ArrayClear()。

更新:

categories = ormExecuteQuery("from Category where Id IN (:Ids)",
                             {Ids=listToArray(FORM.categoryIDs)});
entry.setCategories(categories);
于 2012-04-10T18:20:48.293 回答