想象一下,您有一个与实体具有一对一关系的Person实体。在hibernate中映射时,您的Person类具有Account属性。
现在假设一个Web应用程序发送它想要更新的Person对象,但执行此操作的模块并不关心其嵌套的Account对象(现在),并且不能负担跟踪这些(因为它们不是特别轻,因此取出设置为LAZY)。
由于这些原因,您可以将Person上的Account属性设置为updatable = false,insertable = false,这样如果您发送具有空帐户的Person,则不会导致删除该关系。到目前为止一切都那么好,但当你不想更新账户属性时会发生什么,你会怎么做?有没有办法绕过可插入/可更新?
由于
更新 添加了示例代码(另外,我在谈论实体帐户,但后来由于某种原因我开始将其称为用户,并对其进行了更正)
@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
}
答案 0 :(得分:2)
为 idAccount添加单独的属性,当您要更新Person
时,从数据库中获取帐户的ID并进行设置。
@Column(name="idAccount")
private Integer idAccount;