java - How to work with JPA and the Entity Object Life Cycle -
i'm working on simple surveysystem using jpa eclipselink, , i'm quite new @ (jpa , databases) i'm not sure approach should take.
the way i've done things far using dao-class database-logic, , entity-relations (hopefully) annotated correctly.
my teacher-entity can have multiple surveys, , each survey-entity can have multiple questions. how i've got relation-annotations looking:
teacher.java
@onetomany(mappedby = "teacher", cascade = cascadetype.all) private list<survey> surveys = new arraylist<survey>();
survey.java
@manytoone @joincolumn(name = "teacherid", referencedcolumnname = "id") private teacher teacher; @onetomany(mappedby = "survey", cascade = cascadetype.all) private list<question> questions = new arraylist<question>();
question.java
@manytoone @joincolumn(name = "surveyid", referencedcolumnname = "id") private survey survey;
in each class, i've got addsometing()-method this: survey.addquestion(q)
public void addquestion(question q) { if (q != null) { q.setsurvey(this); questions.add(q); } }
and then, finally, addsomething()-method each entity in dao looks this:
public void addsurvey(survey survey) { entitymanager em = emf.createentitymanager(); entitytransaction tx = em.gettransaction(); try { tx.begin(); em.persist(survey); tx.commit(); } catch (exception e) { e.printstacktrace(); if (tx.isactive()) { tx.rollback(); } } { em.close(); } }
when run code, manage create , add teacher-object database fine. create question-object , add new survey-object , add teacher works fine well. when try persist question , survey objects database , merge/update teacher-object invalidstateexception:
encountered unmanaged object "no.hib.dat104.obl3.model.survey@52892ae7" in life cycle state unmanaged while cascading persistence via field "no.hib.dat104.obl3.model.question.survey" during flush. however, field not allow cascade persist. cannot flush unmanaged objects or graphs have persistent associations unmanaged objects.
i don't think way i'm doing these things correct way work jpa entities. give me short explanation how should or point me in right direction? know entity object life cycle, i'm not sure how approach in project.
the entitymanager context obtaining within addsurvey method new , empty. presumably when persist survey, has questions , teacher assigned. in model, teacher instance existing object in database , reference survey teacher has no cascade options- jpa required throw exception when comes across unmanaged object in model, doesn't know intend it.
there many ways deal in model, depend on many other external factors. quickest solution can think of change things teacher manytoone reference using cascade = cascadetype.merge
option, , use em.merge(survey);
in addsurvey method. jpa determine survey , questions new , insert them, while merge changes in existing teacher instance database.
there many other options though such using extended persistence unit can used read in teacher instance persist survey , question instances. best application depends on expect happen under different circumstances.
Comments
Post a Comment