Question

I'm working on a blog application using ColdBox 3.5 and ColdFusion 10 ORM, and I'm randomly getting following error message:

Error Type: Application : [N/A]

Error Messages: Exception in Hibernate operation. Either the updated/deleted row does not exist or the session contained stale data.

Root cause :org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [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"}
    };
}

Here's the code that I'm running:

<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>

Notice the sections labeled as "works 1 out of 5 times" and "works every time". I don't get what I'm doing wrong. I have other objects similar to these where they use linked table and I'm getting similar error messages. I've reviewed the SQL log. The error appears to occur when it gets ready to insert to EntryCategory table. Any ideas?

Était-ce utile?

La solution

Use HQL and use where ID in (?). That works very well.

If the entity already has an array, use ArrayClear() first.

UPDATE:

categories = ormExecuteQuery("from Category where Id IN (:Ids)",
                             {Ids=listToArray(FORM.categoryIDs)});
entry.setCategories(categories);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top