Hibernate 4.2中,多对一的默认级联映射是none吗?

huangapple 未分类评论47阅读模式
英文:

Hibernate 4.2, is many-to-one default cascade mapping none?

问题

我多次看到默认的 Hibernate cascade"none"。即如果在 XML 映射中省略 cascade 标签,则与 "none" 相同。

今天,当类 A 对类 B 进行了多对一的更新操作时,观察到了奇怪的行为(甚至涉及到了类 B 的关联集合)。

添加 cascade="none" 解决了我的问题。

但我的问题仍然是一样的,即在 Hibernate 的多对一 XML 映射中,默认的级联是什么?

更新日期:2020 年 6 月 5 日 我怀疑默认的 cascade 有一些内部状态(不是 none),在这种模式下 Hibernate 处理具有组件的对象(按 Hibernate 的术语)。在调试会话期间,我观察到了一组“可管理”的对象,它们没有更改,但出于某种未知的原因被标记为“脏”。

TL;DR

XML 映射示例。AuditRecord 是对 User 类的多对一关系。对 AuditRecord 的持久化更新(有时!)User 类和相关集合。

<hibernate-mapping default-lazy="false" default-access="field">
    <class name="package.AuditRecord" table="audit_record">
        <id name="uuid" type="string">
            <column name="uuid" sql-type="char(32)" not-null="true" />
            <generator class="assigned" />
        </id>
        <many-to-one name="user" class="package.User" cascade="none">
            <column name="user_uuid" sql-type="char(32)" not-null="false" />
        </many-to-one>
        <property name="success" type="boolean" not-null="true"/>
        ...
    </class>
</hibernate-mapping>

实际持久化 AuditRecord 类的 Java 代码示例。

Transaction tx = session.beginTransaction();
try {
    session.saveOrUpdate(pObject);
    session.flush();
    tx.commit();
} catch (Exception e) {
    tx.rollback();
    ...
英文:

I saw many times that default hibernate cascade is &quot;none&quot;. I.e. if you omit cascade tag in xml mapping it's the same as &quot;none&quot;.

Today I observed strange behavior when class A that many-to-one to class B updated class B tables (and even dependent B's collections).

Adding cascade=&quot;none&quot; fixed my issue.

But my question remains the same what is default cascade for many-to-one xml mapping in hibernate?

UPDATE 6.05.20 I suspect that default cascade has some internal (not none state) and in that mode hibernate process objects with components (in hibernate terms). During debug session I observed a list of "manageable" objects that are unchanged but mark as "dirty" for some unknown reason.

TL;DR

Sample of xml mapping. AuditRecord is many-to-one to User class. Persistent of AuditRecord updates (sometimes!) User class and dependent collections.

&lt;hibernate-mapping default-lazy=&quot;false&quot; default-access=&quot;field&quot;&gt;
	&lt;class name=&quot;package.AuditRecord&quot; table=&quot;audit_record&quot;&gt;
		&lt;id name=&quot;uuid&quot; type=&quot;string&quot;&gt;
			&lt;column name=&quot;uuid&quot; sql-type=&quot;char(32)&quot; not-null=&quot;true&quot; /&gt;
			&lt;generator class=&quot;assigned&quot; /&gt;
		&lt;/id&gt;
		&lt;many-to-one name=&quot;user&quot; class=&quot;package.User&quot; cascade=&quot;none&quot;&gt;
			&lt;column name=&quot;user_uuid&quot; sql-type=&quot;char(32)&quot; not-null=&quot;false&quot; /&gt;
		&lt;/many-to-one&gt;
		&lt;property name=&quot;success&quot; type=&quot;boolean&quot; not-null=&quot;true&quot;/&gt;
		...
	&lt;/class&gt;
&lt;/hibernate-mapping&gt;

Sample of Java code that actually persist AuditRecord class.

	  Transaction tx = session.beginTransaction();
	  try {
		  session.saveOrUpdate(pObject);
		  session.flush();
		  tx.commit();
	  } catch (Exception e) {
		  tx.rollback();
		  ...

huangapple
  • 本文由 发表于 2020年5月5日 19:04:54
  • 转载请务必保留本文链接:https://java.coder-hub.com/61611640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定