Monday, 15 March 2010

java - A different object with the same identifier value was already associated with the session:. neither merge nor saveOrUpdate works -



java - A different object with the same identifier value was already associated with the session:. neither merge nor saveOrUpdate works -

i have gui list of teachers shown. 2 selected user - going form teachers of new school class gets created. teacher - class relationship n-m.

school class: (it inherits id group)

@entity @table(name="school_classes") @cacheable(true) public class schoolclass extends group{ @manytomany(cascade=cascadetype.all, mappedby="classes", fetch=fetchtype.eager) private sortedset<teacher> teachers;

teacher:

@entity @table(name="teachers") @cacheable(true) public class teacher extends creator.models.school.entity{ // name etc @manytomany(cascade=cascadetype.all, fetch=fetchtype.eager) @jointable(name="class_teachers", joincolumns = @joincolumn(name="teacher_id", referencedcolumnname="id"), inversejoincolumns = @joincolumn(name="class_id", referencedcolumnname="id")) private sortedset<schoolclass> classes;

i seek create new class this:

string classname = request.getparameter("class_name"); string id1 = request.getparameter("class_teacher1"); string id2 = request.getparameter("class_teacher2"); teacher t1 = dataretrievemodule.getteacher(id1); teacher t2 = dataretrievemodule.getteacher(id2); layout l = new layout(); schoolclass newclass = new schoolclass(classname); newclass.setlayout(l); newclass.addteacher(t1); t1.addclass(newclass); newclass.addteacher(t2); t2.addclass(newclass); datainsertionmodule.insert(newclass);

this statement dataretrievemodule.getteacher(id1) opens session, retrives teacher id , closes it. datainsertionmodule.insert(newclass) opens session, calls session.saveorupdate(newclass). (i tried session.merge(newclass))

retrieving teacher:

public static teacher getteacher(string id) { session session = null; teacher t = null; try{ sessionfactory = myfactory.getsessionfactory(); session = sessionfactory.opensession(); t = (teacher) session.get(teacher.class, long.parselong(id)); } catch(exception e) { system.out.println("in dao:"); e.printstacktrace(); if(session!=null) session.close(); } finally{ if(session!=null) session.close(); } homecoming t; }

data insertion:

public static void insert(object o) { session session = null; seek { sessionfactory = myfactory.getsessionfactory(); session = sessionfactory.opensession(); transaction tx = session.begintransaction(); session.save(o); tx.commit(); system.out.println("insertion done"); } grab (exception e) { system.out.println(e.getmessage()); e.printstacktrace(); } { if(session!=null) session.close(); } }

but insertion never works. there object same id exists.

a different object same identifier value associated session: [creator.models.school.teacher#3]

i searched on stackoverflow , have overridden gethashcode , equals method in class business objects inherit from:

@override public int compareto(entity o) { if(this.id < o.getid()) homecoming -1; else if(this.id > o.getid()) homecoming 1; homecoming this.getclass().getname().compareto(o.getclass().getname()); } @override public boolean equals(object o) { if(o instanceof entity) homecoming this.compareto((entity)o)==0; else homecoming this.equals(o); } @override public int hashcode() { char[] bla = this.getclass().getname().tochararray(); int blue=0; for(char c:bla) bluish = blue*10 + c; homecoming (int) (id+blue); }

furthermore tried alter cascadetype of manytomany merge (reverted again).

at moment merge teacher objects after retrieving both of them session.get(..), retrieving takes lot of info out of db. there futher ..tomany relations. hence might happen, phone call causes both teachers loaded.

public static object merge(object o) { session session = null; object returnval = null; seek { sessionfactory = myfactory.getsessionfactory(); session = sessionfactory.opensession(); transaction tx = session.begintransaction(); returnval = session.merge(o); tx.commit(); } grab (exception e) { system.out.println(e.getmessage()); e.printstacktrace(); } { if(session!=null) session.close(); } homecoming returnval; }

.

teacher t1 = dataretrievemodule.getteacher(id1); teacher t2 = dataretrievemodule.getteacher(id2); t1= datainsertionmodule.merge(t1); t2= datainsertionmodule.merge(t2);

therefore thought, if merge 1 get-method returned 1 must have been loaded phone call other teacher, should work. (like here: http://www.stevideter.com/2008/12/07/saveorupdate-versus-merge-in-hibernate/) not :(

is because object of same superclass (object or entity class) has same id?

please help me!

one potential situation lead error when id of teacher 1 , teacher 2 same. since close session between each detached, , end loading 2 different objects both represent same row in database (they have same primary key.) then, when these both going reattached session through schoolclass beingness saved, hibernate see 2 different objects both representing same row in database, , not know 1 represent right state persist database.

don't know sure if case here, should run debugger , check state of objects referenced teacher1 , teacher2.

java hibernate java-ee jpa

No comments:

Post a Comment