问题
如果某个字段是注释insertable=false, updatable=false
,那是不是意味着你不能插入值也不能更改现有值?你为什么想这么做?
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany(mappedBy="person", cascade=CascadeType.ALL)
private List<Address> addresses;
}
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name="ADDRESS_FK")
@Column(insertable=false, updatable=false)
private Person person;
}
#1 热门回答(86 赞)
当创建/更新相关实体的195050117责任**不具有相关实体时,你会这样做。例如。你有aPerson
和anAddress
。你想在352127580实体中添加4652114851到@OneToMany
关系与Person
实体,只是因为创建或更新aPerson
并不是Address
实体的责任。反过来说。
#2 热门回答(71 赞)
当你需要在实体中多次映射字段时,Defininginsertable=false, updatable=false
非常有用,通常:
- 使用复合键时
- 使用共享主键时
- 使用级联主键时
这是IMO不是一个语义的东西,但绝对是技术的。
#3 热门回答(17 赞)
我想补充一下BalusCandPascal Thiventanother常用的insertable=false, updatable=false
的答案:
考虑一个不是anid的列,但是某种4055837884序列号为**.计算序列号的责任可能不一定属于应用程序。
例如,序列号从1000开始,每个新实体应增加1。这在数据库中很容易完成,非常恰当,在这种情况下,这些配置是有意义的。