public class Application{
@Id
private Long id;
@OneToMany(mappedBy = "application")
private List<Licence> licences = new ArrayList<Licence>();
...
}
public class Licence{
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID", nullable = false)
private Application application;
...
}
Run Code Online (Sandbox Code Playgroud)
当我去 em.merge(application); 时,如何让 hibernate 将许可证保留在应用程序对象中,而不是尝试保留许可证;
我不是要在应用程序中使用级联保存许可证。许可证在真正持久化之前有很多业务规则要运行,因此我将分别调用每个许可证的persist。我该怎么做呢 ?这适用于持久化,但不适用于合并。
在合并我不断得到
org.hibernate.TransientObjectException: object is an unsaved transient
instance - save the transient instance before merging: com.cmr.Licence
Run Code Online (Sandbox Code Playgroud)
使用insertable = false, updatable = false. 更新这个
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID", nullable = false)
private Application application;
Run Code Online (Sandbox Code Playgroud)
到
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID", nullable = false, insertable = false, updatable = false)
private Application application;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1961 次 |
| 最近记录: |