简体   繁体   English

休眠不更新(合并)对象

[英]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>

Watch out for mutable="false" it makes the object imutable by the application: 当心mutable="false"它使该对象可被应用程序使用:

As specified here: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html 如此处指定: 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. 可变的(可选-默认为true):指定该类的实例是可变的(不可变)。 Immutable classes, mutable="false", cannot be updated or deleted by the application. 不可变的类mutable =“ false”无法由应用程序更新或删除。 This allows Hibernate to make some minor performance optimizations. 这使Hibernate可以进行一些次要的性能优化。

Removing mutable="false" from the hbm file fixed the problem. 从hbm文件中删除mutable="false"可以解决此问题。

Have you tried setting (and saving) the Tag on the item, instead of adding items to the Tag class? 您是否尝试过在商品上设置(并保存)标签,而不是将商品添加到Tag类中?

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. 因此,基本上可以完成数据库所需的工作,首先创建一个Tag(不引用任何项目),然后再创建对Tag的引用的项目(记录)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM