MatBanik
MatBanik

Reputation: 26860

Hibernate not updating (merging) object

I have tag object that is driving me crazy. I'm trying to update it and every time I do everything seems OK until I check the database and it is not updated.

I'm having all the logging turned on but I don't see anything out of the ordinary.

Even after I create brand new object and try to update (or merge it) right after, it will not show in database. It will create the new object but it will not update it.

Did anyone have similar problem and how did you solve it?

<hibernate-mapping package="com.package">

<class name="com.package.Tag" table="tags" lazy="false" mutable="false" >
    <meta attribute="generated-class">com.package.generated.AbstractTag</meta>
    <meta attribute="scope-class">public abstract</meta>
    <cache usage="read-write"/>

    <id name="id" type="long" column="tag_id">
        <generator class="native"/>
    </id>


    <property name="name" type="string" column="name" unique="true"/>

    <property name="itemCount" type="integer" column="itemCount"/>

</class>

</hibernate-mapping>

Upvotes: 2

Views: 2417

Answers (2)

MatBanik
MatBanik

Reputation: 26860

Watch out for mutable="false" it makes the object imutable by the application:

As specified here: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html

mutable (optional - defaults to true): specifies that instances of the class are (not) mutable. Immutable classes, mutable="false", cannot be updated or deleted by the application. This allows Hibernate to make some minor performance optimizations.

Removing mutable="false" from the hbm file fixed the problem.

Upvotes: 1

M Platvoet
M Platvoet

Reputation: 1654

Have you tried setting (and saving) the Tag on the item, instead of adding items to the Tag class?

So basically doing what is required by the database, first creating a Tag (without any reference to items) and then creating items (records) with references to the Tag.

Upvotes: 0

Related Questions