Wednesday, 15 September 2010

Java & Hibernate - PersistEventListener does not set value properly -



Java & Hibernate - PersistEventListener does not set value properly -

i have issue hibernate:

i have basic dao saves entity next way:

@override public void save(baseentity entity) { session session = this.sessionfactory.opensession(); transaction trans = session.begintransaction(); session.persist(entity); trans.commit(); session.close(); }

i've registered basic event listener persisting entity:

@component public class owlpresavelistener implements persisteventlistener { @override public void onpersist(persistevent persistevent) throws hibernateexception { object o = persistevent.getobject(); if (o instanceof baseentity) { baseentity entity = (baseentity)o; if (entity.getcreated() == null) { entity.setcreated(new date()); } entity.setupdated(new date()); } } }

when seek persist entity not set create date though mandatory because assume listener set before persist. when running in debug mode eventlistener triggered expected. , when examining entity values set.

there logs indicating right values associated entity:

23:49:40.545 [main] debug o.h.e.i.abstractflushingeventlistener - flushed: 1 insertions, 1 updates, 0 deletions 1 objects 23:49:40.545 [main] debug o.h.e.i.abstractflushingeventlistener - flushed: 0 (re)creations, 0 updates, 0 removals 0 collections 23:49:40.546 [main] debug o.h.internal.util.entityprinter - listing entities: 23:49:40.546 [main] debug o.h.internal.util.entityprinter - com.owl.core.model.user{id=89672b6e-4f8e-4bfc-b24c-3cb3f853cad9, email=v9i0t0sg@dgs88uab.6p8, updated=fri oct 24 23:49:40 cest 2014, created=fri oct 24 23:49:40 cest 2014, name=9v9nduqfpsmn0 fjpufh2c713q2, deleted=null}

note 'created' date not empty.

regardless next message time , time again:

caused by: com.mysql.jdbc.exceptions.jdbc4.mysqlintegrityconstraintviolationexception: column 'created' cannot null spring : 4.1.1.release hibernate: 4.3.5.final

i've tried saveorupdate event listener no luck.

it setting different value within event listener has no effect when persisting entity.

not quite sure goind on can please suggest something?

please seek @dynamicinsert , see if error dissapear. maybe examine table schema specially created column set default value , see happens.

java hibernate jpa

No comments:

Post a Comment