JPA级联配置

原作写于2014-05-24 02:00:39

@OneToMany(mappedBy = "user")

private Set < Company > companies;

 

@ManyToOne(cascade = {CascadeType.MERGE},fetch = FetchType.EAGER)
@JoinColumn(name = "USER_ID")
private User user;

 

CascadeType.PERSIST: cascades the persist (create) operation to associated entities persist() is called or if the entity is managed

CascadeType.MERGE: cascades the merge operation to associated entities if merge() is called or if the entity is managed

CascadeType.REMOVE: cascades the remove operation to associated entities if delete() is called

CascadeType.REFRESH: cascades the refresh operation to associated entities if refresh() is called

CascadeType.DETACH: cascades the detach operation to associated entities if detach() is called

CascadeType.ALL: all of the above

 

1:临时态,英文名称:Transient

临时态故名思义就是临时的,它随时将被垃圾回收,而且对数据库没有对应关系。
使用new来创建对象,非持久化,不存入Session。
2:持久态,英文名称:Persistent

已持久化,存入Session,所保存的对象通过GET或Load方法或获取对象。
3:游离态,英文名称:Detached

已持久化,但已被清除出Session,处于游离失散状态。
当对象处于游离态(Detached)时,修改其属性,是不会与数据库发生同步的。调用Update()方法后,对象则变回持久态(Persistent)
 
因此当给Company赋一个已经存在于数据库中的Detached User并且persist Company时级联关系是不能包括CascadeType.PERSIST的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值