java - How to update the Hibernate Entity Record when only few fields of entity obj are modified and remaining fields should not be null -
is there other different solution code. every pojo class have check modified data coming browser , store modified data database.
see below billingtax obj coming browser updated data , billingtaxdbobject obj retrieved database , check if condition whether updated data changed or not
if pojo class has 20 fields, have write 20 if conditions if pojo class has 5 fields, have write 5 if conditions
instead of writing if conditions checking wheter data modified or nor there other simplest way?
@override public billingtax update(billingtax billingtax) throws datainsufficientexception, recordnotfoundexception { log.debug("billingtaxserviceimpl.update()...................."); try { if (billingtax == null) throw new datainsufficientexception("billingtax object null"); billingtax billingtaxdbobject = get(billingtax.getid()); if (billingtaxdbobject == null) throw new recordnotfoundexception("billingtax object not found in database"); if (billingtax.gettaxapplytype() != null && !billingtax.gettaxapplytype().equals(billingtaxdbobject.gettaxapplytype())) billingtaxdbobject.settaxapplytype(billingtax.gettaxapplytype()); if (billingtax.getcode() != null && !billingtax.getcode().trim().equalsignorecase("null") && !billingtax.getcode().equalsignorecase(billingtaxdbobject.getcode())) billingtaxdbobject.setcode(billingtax.getcode()); if (billingtax.getname() != null && !billingtax.getname().trim().equalsignorecase("null") && !billingtax.getname().equalsignorecase(billingtaxdbobject.getname())) billingtaxdbobject.setname(billingtax.getname()); if (billingtax.getdescription() != null && !billingtax.getdescription().trim().equalsignorecase("null") && !billingtax.getdescription().equalsignorecase(billingtaxdbobject.getdescription())) billingtaxdbobject.setdescription(billingtax.getdescription()); if (billingtax.getservicetypefortax() != null && !billingtax.getservicetypefortax().equals(billingtaxdbobject.getservicetypefortax())) billingtaxdbobject.setservicetypefortax(billingtax.getservicetypefortax()); if (billingtax.gettaxvalue() != null && !billingtax.gettaxvalue().equals("null") && !billingtax.gettaxvalue().equals(billingtaxdbobject.gettaxvalue())) billingtaxdbobject.settaxvalue(billingtax.gettaxvalue()); if (billingtax.getstatus() != null && !billingtax.getstatus().equals(billingtaxdbobject.getstatus())) billingtaxdbobject.setstatus(billingtax.getstatus()); if (billingtax.getorderno() != null && !billingtax.getorderno().equals("null") && !billingtax.getorderno().equals(billingtaxdbobject.getorderno())) billingtaxdbobject.setorderno(billingtax.getorderno()); if (billingtax.getid() != null && !billingtax.getid().trim().equalsignorecase(billingtaxdbobject.getid()) && !billingtax.getid().equalsignorecase(billingtaxdbobject.getid())) billingtaxdbobject.setid(billingtax.getid()); if (billingtax.getstartdate()!= null && !billingtax.getstartdate().equals(billingtaxdbobject.getstartdate())) billingtaxdbobject.setstartdate(billingtax.getstartdate()); if (billingtax.getenddate()!= null && !billingtax.getenddate().equals(billingtaxdbobject.getenddate())) billingtaxdbobject.setenddate(billingtax.getenddate()); billingtaxdbobject.setupdateddate(new date()); return billingtaxdao.update(billingtaxdbobject); } catch (exception e) { log.error("billingtaxserviceimpl.update()....................exception:" + e.getmessage()); throw e; } }
you can dynamic updates hibernate if can avoid check changes among dto , entity , update fields come web. if need check dto web , entity can use apache bean util find changed values (or use spring util if have or reflection java...) , update dynamic updates.
see : beanutils
beanutils.copyproperties() // there 3 methods.
check how works in source code . create own util method , similar beanutils.copyproperties() , logic need (not null , not equal source-entity value ).
also use method beanutils , propertydescriptor :
public static propertydescriptor[] getpropertydescriptors(class clazz) throws beansexception
iterate on array of propertydescriptor , check need (set value source reflectionutils).
with approach populate properties not null , changed( if need it) billingtaxdbobject , update it.
you can put copy / merge method util class , reuse place need copy dto entity checks.
Comments
Post a Comment