java - Annotation mapping vs. XML mapping and deleting an entity -
i working migrating project pure jpa annotation based mapping xml mapping , running issue when trying delete (remove) , entity , children. works in xml mapping , not work annotation mapping.
the xml mapping looks this:
<set name="evaluations" order-by="evaldate desc" table="evaluation" lazy="true" inverse="true" cascade="delete"> <key column="requestid" /> <one-to-many class="org.stuff.model.evaluation" /> </set>
the annotation mapping, far can tell this:
@onetomany(orphanremoval=true) @joincolumn(name = "requestid") @orderby("evaldate desc") private set<evaluation> evaluations = new treeset<>();
this uni-directional relationship.
the jpa code delete entity is:
servicerequest sr = em.getreference(servicerequest.class, id); em.remove(sr);
where above evaluation
child object of servicerequest
. hibernate 4.3.7 jpa impl using, running on wildfly 8.2.
with hibernate set barf out sql, executing remove annotation mapping in place hibernate produces query entity reference , when remove
called produce update trying update child record in evaluation
fk servicerequest null:
hibernate: update evaluation set requestid=null requestid=?
and blows because there not null
constraint on requestid
.
if same operation using xml mapping (see above snippet) works fine. child entities deleted along parent. , hibernate produces selects
, deletes
, if never tries update anything.
this feels have annotation mapping wrong, cannot figure went wrong. please help.
you xml config says relationship between servicerequest , set bi-directional because inverse = "true".
but jpa annotation uni-directional. should work (edited after op's comment)
@onetomany(orphanremoval=true,mappedby="requestid") @orderby("evaldate desc") private set<evaluation> evaluations = new treeset<>();
here mappedby="requestid"
tells hibernate owner side of relationship. issue statement remove evaluation.
Comments
Post a Comment