hibernate - How to save only changed columns in JPA spring-boot -


this question has answer here:

this has absolutely nothing todo duplicate linked question!

my problem jpa trying save fields, though has not changed them, ruins things me on db2.

the entity:

@data  // auto getter , setters lombok @entity @idclass(tkmstpid.class) @table(name="tkmstp") public class tkmstp implements serializable {      // dmd column: tkslsk: decimal(2)     @id     @column(name="tkslsk")     private long slsknr;     // dmd column: tkknnr: decimal(7)     @id     @column(name="tkknnr")     private long kundenr;     // dmd column: tknr: decimal(7)     @id     @column(name="tknr")     private long tanknummer;     // dmd column: tkstat: decimal(1)     @column(name = "tkevol")     private long estaarsforbrug;     @column(name = "tkgdk")     private long gkode;     @column(name = "tkudssky")     private string udskrivkortjn;     @column(name="tklv5dat")     private java.sql.date leveringsdato_tklv5dat;     // dmd column: tkudssky: char(1)  } 

the handling:

tkmstpid id = new tkmstpid();                 id.setkundenr(long.parselong(customer.getaccountnumber()));                 id.setslsknr(1l);                 id.settanknummer(customer.getcontainerid());                 tkmstp container = tkmstprepository.getone(id);                 if (container != null) {                     boolean changed = false;                     if (container.getestaarsforbrug() == null || container.getestaarsforbrug() == 0l) {                         changed = setcontainerestimatedvalues(container, customer);                     } else if (container.getgkode() != 5l && container.getgkode() != 6l) {                         changed = setcontainerestimatedvalues(container, customer);                     }                     if (changed) {                         tkmstprepository.save(container);                         system.out.println("updated container info on: customer: " + id.getkundenr() + " , container: " + id.gettanknummer());                     }                 } 

sql generated:

hibernate: update tkmstp set tklv5dat=?, tkudssky=?, tkevol=?, tkgdk=? tkknnr=? , tkslsk=? , tknr=? 

sql i'd like(without tklv5dat)
update tkmstp set tkudssky=?, tkevol=?, tkgdk=? tkknnr=? , tkslsk=? , tknr=?

the error:
java.sql.sqlexception: [sql0407] null values not allowed in column or variable tklv5dat.

thanks in advance all...

application.properties jpa settings:

 spring.jpa.database-platform=org.hibernate.dialect.db2dialect  spring.jpa.hibernate.ddl-auto = false spring.jpa.show-sql=true   spring.datasource.driver-class-name=com.ibm.as400.access.as400jdbcdriver 

imo must not allowed, either field transient , not saved @ or if use orm-layer, object must saved fields make sure integrity of object guaranteed. think way achieve want use explicite update statements.

em.createquery(    "update tkmstp t         set t.tkudssky=:tkudssky, t.tkevol=:tkevol, t.tkgdk=:tkgdk         t.tkknnr=:tkknnr , t.tkslsk=:tkslsk , t.tknr=:tknr")    .setparameter(":tkudssky",container.tkudssky)    .setparameter(":tkevol",container.tkevol)    .setparameter(":tkgdk",container.tkgdk)    .setparameter(":tkknnr",container.tkknnr)    .setparameter(":tkslsk",container.tkslsk)    .setparameter(":tkudssky",container.tkudssky)    .executeupdate(); 

the other possibility use 1 update direct using dbms-means set tklv5dat correct default value. positive side-effect be, dbms-checks , jpa-checks same.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -