java - Hibernate @OneToOne @NotNull -
is valid declare @onetoone
, @notnull
on both sides of relationship, such as:
class changeentry { @onetoone(cascade=cascadetype.all) @notnull changeentrydetails changeentrydetails; public void adddetails(changeentrydetails details) { this.changeentrydetails = details; details.setchangeentry(this); } } class changeentrydetails { @onetoone(cascase=cascadetype.all) @notnull changeentry changeentry; public void setchangeentry(changeentry changeentry) { this.changeentry = changeentry; } }
i can't find says invalid, seems during persistence @ least 1 side of relationship must violated. (eg., if writing changeentry first, changeentrydetails null temporarily).
when trying this, see exception thrown not-null property references null or transient value
.
i'd avoid relaxing constraint if possible, because both sides must present.
is valid declare
@onetoone
,@notnull
on both sides of relationship (...) can't find says invalid, seems during persistence @ least 1 side of relationship must violated. (e.g. if writingchangeentry
first,changeentrydetails
null temporarily).
it valid , works fine mapped entities. need declare 1 side of bi-directional association "owning" side (this "control" order of inserts). 1 possible working solution:
@entity @namedqueries( { @namedquery(name = changeentry.find_all_changeentries, query = "select c changeentry c") }) public class changeentry implements serializable { public final static string find_all_changeentries = "findallchangeentries"; @id @generatedvalue private long id; @onetoone(optional = false, cascade = cascadetype.all) @joincolumn(name = "details_id", unique = true, nullable = false) @notnull private changeentrydetails changeentrydetails; public void adddetails(changeentrydetails details) { this.changeentrydetails = details; details.setchangeentry(this); } // constructor, getters , setters }
and other entity (note mappedby
attribute set on non-owning side of association):
@entity public class changeentrydetails implements serializable { @id @generatedvalue private long id; @onetoone(optional = false, mappedby = "changeentrydetails") @notnull private changeentry changeentry; // constructor, getters , setters }
with these entities, following test (for demonstration purposes) passes:
public class changeentrytest { private static entitymanagerfactory emf; private entitymanager em; @beforeclass public static void createentitymanagerfactory() { emf = persistence.createentitymanagerfactory("testpu"); } @afterclass public static void closeentitymanagerfactory() { emf.close(); } @before public void begintransaction() { em = emf.createentitymanager(); em.gettransaction().begin(); } @after public void rollbacktransaction() { if (em.gettransaction().isactive()) { em.gettransaction().rollback(); } if (em.isopen()) { em.close(); } } @test public void testcreateentrywithoutdetails() { try { changeentry entry = new changeentry(); em.persist(entry); fail("expected constraintviolationexception wasn't thrown."); } catch (constraintviolationexception e) { assertequals(1, e.getconstraintviolations().size()); constraintviolation<?> violation = e.getconstraintviolations() .iterator().next(); assertequals("changeentrydetails", violation.getpropertypath() .tostring()); assertequals(notnull.class, violation.getconstraintdescriptor() .getannotation().annotationtype()); } } @test public void testcreatedetailswithoutentry() { try { changeentrydetails details = new changeentrydetails(); em.persist(details); fail("expected constraintviolationexception wasn't thrown."); } catch (constraintviolationexception e) { assertequals(1, e.getconstraintviolations().size()); constraintviolation<?> violation = e.getconstraintviolations() .iterator().next(); assertequals("changeentry", violation.getpropertypath() .tostring()); assertequals(notnull.class, violation.getconstraintdescriptor() .getannotation().annotationtype()); } } @test public void validentrywithdetails() { changeentry entry = new changeentry(); changeentrydetails details = new changeentrydetails(); entry.adddetails(details); em.persist(entry); query query = em.createnamedquery(changeentry.find_all_changeentries); assertequals(1, query.getresultlist().size()); } }
Comments
Post a Comment