Hibernate @ManyToOne仅适用于CascadeType.ALL [英] Hibernate @ManyToOne only works with CascadeType.ALL
问题描述
我正在使用Hibernate 3.3.1,我想创建人员和分配公司之间的关系。他们应该松散耦合,但我想安排
通过级联创建公司,而不是明确调用saveOrUpdate(newCompany)。
I am using Hibernate 3.3.1 and i would like to create a relation between persons and an assigned company. They should be loosely coupled, but i would like to arrange to create a company via cascade and not explicitly calling saveOrUpdate(newCompany).
我定义了下列实体:
I defined the following entities:
class Company
{
@Id
Long companyId;
String name;
}
class Person
{
@Id
Long personId;
String name;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
Company company;
}
里面我做了以下操作:
inside my dao i am doing the following:
testpers.setCompany (newCompany);
session.saveOrUpdate(testpers);
我得到一个异常
org.hibernate.TransientObjectException:对象引用一个未保存的
瞬态实例 - 在刷新之前保存瞬态实例:
consearch.model.core.Company
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: consearch.model.core.Company
当使用cascade = CascadeType.ALL进行注释时,它可以正常工作,但我也不想同时使用
ccade删除(例如,如果删除了公司,人不应该被删除)
When annotating with "cascade = CascadeType.ALL" it works, but i do not want to also ccade Deletion (e.g. if a Company is removed, then the person should not be removed)
我想知道没有人在
之前有这个问题,先谢谢你帮助我。
Shane
I was wondering that nobody had this problem before Thanks in advance for helping me. Shane
推荐答案
可能需要启用Hibernate自定义 @Cascade
使用非JPA <$ c $时c> Session.saveOrUpdate()方法。
添加 @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
或使用 Session.persist()
Probably you need to enable Hibernate custom @Cascade
when using non-JPA Session.saveOrUpdate()
method.
Add @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
or use Session.persist()
这篇关于Hibernate @ManyToOne仅适用于CascadeType.ALL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!