原作写于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的