java - Hibernate : object references an unsaved transient instance - save the transient instance before flushing -


i receive following error when try list using hibernate + postgresql

myobject:

@entity @table(name = "my_objects", schema = "public") public class myobject implements java.io.serializable {  private static final long serialversionuid = 1l; @id @column(name = "my_object_id", unique = true, nullable = false) @generatedvalue private long myobjectid;  @manytoone(cascade = cascadetype.all, fetch = fetchtype.lazy) @joincolumn(name = "owner_id") private user user;  @column(name = "scheme_zone_name", length = 150) private string schemezonename;  @temporal(temporaltype.date) @column(name = "create_dt", length = 13) private date createdt;  @column(name = "comment", length = 250) private string comment;   public myobject() { }  public myobject(long myobjectid) {     this.myobjectid = myobjectid; }  public myobject(long myobjectid, user user,  string myobjectname, date createdt, string) {     this.schemezoneid = schemezoneid;     this.user = user;     this.myobjectname = myobjectname;     this.createdt = createdt;     this.comment = comment; } ....getters/setters .....} 

user :

@entity @table(name = "dp_users", schema = "public") public class user implements java.io.serializable {  private static final long serialversionuid = 1l; @id @column(name = "user_id", unique = true, nullable = false, precision = 5, scale = 0) @generatedvalue private int userid;  @column(name = "user_login", nullable = false, length = 25) private string userlogin;  @column(name = "user_password", nullable = false, length = 25) private string userpassword;  @onetomany(cascade = cascadetype.all, fetch = fetchtype.lazy, mappedby = "user") private set<myobject> myobjects = new hashset(0);  public user() { }  public user(int userid, string userlogin, string userpassword) {     this.userid = userid;     this.userlogin = userlogin;     this.userpassword = userpassword; }  public user(int userid, string userlogin, string userpassword, set<myobject> myobjects) {     this.userid = userid;     this.userlogin = userlogin;     this.userpassword = userpassword;     this.myobjects = myobjects; } ....getters/setters .....} 

at first user , save in user object:

public user login(string login, string password) {     session session = null;     try {         session = hibernateutil.getsessionfactory().opensession();         return (user) session.createcriteria(user.class)                 .add(restrictions.eq("userlogin", login).ignorecase())                 .add(restrictions.eq("userpassword", password)).uniqueresult();     } catch (exception e) {         e.printstacktrace();     } {         if (session != null && session.isopen()) {             session.close();         }     }     return null; } 

then, when try list of myobject user :

public list<myobject> getmyobjects(user user) throws sqlexception {     list<myobject> o = null;     session session = null;     try {         session = hibernateutil.getsessionfactory().opensession();         o = session.createcriteria(myobject.class)                 .add(restrictions.eq("user", user))                 .addorder(order.asc("myobjectname"))                 .list();     } catch (exception e) {         e.printstacktrace();     } {         if (session != null && session.isopen()) {             session.close();         }     }     return o; } 

i receive

org.hibernate.transientobjectexception: object references unsaved transient instance - save transient instance before flushing: cc.testproject.common.user 

what have done wrong?

you should use same session retrieve entities. session scope must match unit of work, don't instantiate each operation.

see struggling understand entitymanager proper use


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -