【发布时间】:2011-11-09 22:33:26
【问题描述】:
假设您有一个 Person 实体,它与一个 Account 实体具有一对一的关系。在休眠中映射它时,您的 Person 类有一个 Account 属性。
现在假设一个 Web 应用程序发送了它想要更新的 Person 对象,但是执行此操作的模块并不关心其嵌套的 Account 对象(目前),并且它不能负担跟踪这些(因为它们不是特别轻量级,因此提取设置为 LAZY)。
由于这些原因,您可以将 Person 上的 Account 属性设置为可更新=false、可插入=false,这样如果您发送带有空帐户的 Person 就不会导致关系被删除。到目前为止一切顺利,但是当你不想更新 Account 属性时会发生什么,你会怎么做?有没有办法绕过可插入/可更新?
谢谢
更新 添加了示例代码(另外,我说的是实体 Account,但后来出于某种原因我开始将其称为 User,也更正了)
@Entity
@Table(name="person")
public class Person implements java.io.Serializable
{
//properties
private Integer id;
private String Name;
//... this class would have many properties
/*Most of the time the frontend will send a Person object
that has this property set to null even if it exists
because the screen that handles this doesn't manage accounts
There are other screens that will send that complete person object with
its account, so the problem is how to save an incomplete person object
without deleting the relationship
*/
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="idAccount", insertable=false, updatable=false)
public Account getAccount()
{
return this.account;
}
public void setAccount(Account account)
{
this.account = account;
}
}
@Entity
@Table(name="account")
public class Account implements java.io.Serializable
{
//properties
private Integer idAccount;
//this class has even more properties and nested objects
}
【问题讨论】:
-
想发布一些伪代码?我不确定我是否正确理解你的意思。
标签: hibernate hibernate-mapping