我有以下DO

@Entity
@Table(schema = "USERCVT", name = "Table1")
public class DeviceMasterDO implements Serializable{

@OneToMany(cascade = CascadeType.ALL, mappedBy = "deviceMasterDO", orphanRemoval = true)
private List<InOutTransactionDO> inOutTransactionDOs;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "deviceMasterDO", orphanRemoval = true)
private List<AdminTransactionDO> adminTransactionDOs;
/*************MORE CODE**************/
}


当我尝试更新DeviceMasterDO时,它也在InOutTransactionDO和AdminTransactionDO中执行更新。我只想更新DeviceMasterDO。我该如何实现?

问题:
如果DeviceMasterDO的InOutTransactionDO和AdminTransactionDO都不为null,则更新工作正常并执行-

Hibernate: update schema.Table1 set .....
Hibernate: update schema.Table2 set ....
Hibernate: update schema.Table3 set ...


如果DeviceMasterDO的InOutTransactionDO和AdminTransactionDO为null,则更新失败-

Hibernate: insert into usercvt.Table2 (a,b,c) values (?, ?, ?)
2016-06-14 17:51:44 ERROR SqlExceptionHelper:146 - [SQL0407] Null values not allowed in column or variable b.

最佳答案

您是否可以考虑删除以下内容:

cascade = CascadeType.ALL

请声明inOutTransactionDOs如下:

@OneToMany(mappedBy = "deviceMasterDO", orphanRemoval = true)
private List<InOutTransactionDO> inOutTransactionDOs;


adminTransactionDOs如下:

@OneToMany(mappedBy = "deviceMasterDO", orphanRemoval = true)
private List<AdminTransactionDO> adminTransactionDOs;

10-01 07:30