Search code examples
javasqlhibernatehibernate-mapping

How to simulate the "ON UPDATE NO ACTION" in Hibernate


I have the following 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**************/
}

When I am trying to update DeviceMasterDO, it performs an update in InOutTransactionDO and AdminTransactionDO as well. I want to update only DeviceMasterDO. How can I achieve that ?

Issue: If DeviceMasterDO has both InOutTransactionDO and AdminTransactionDO as not null then update works fine and executes -

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

If DeviceMasterDO has either InOutTransactionDO and AdminTransactionDO as null then update fails -

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. 

Solution

  • Would you please consider removing the following:

    cascade = CascadeType.ALL

    Please declare inOutTransactionDOs as follows:

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

    and adminTransactionDOs as follows:

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