java - Creating an instance of a class and at the same time create an instance of another class that depends on the first class -


this question has answer here:

edit:how question different 1 linked?

i think question different because seems if caused jpa trying add user same id, because of foregin key value in class (student) being added. issue linked seems caused not generateing ids automatically.

i have method creates user , returns user. pass user method create student. user student. can't because :
org.postgresql.util.psqlexception: error: duplicate key value violates unique constraint "pk_user_id" detail: key (user_id)=(7001) exists.

my methods in backing bean looks this:

public users2 adduser(string username, string password, string emailadress,      string firstname, string lastname) {      users2 u = new users2();     u.setusername(username);     u.setpassword(password);     u.setemailaddress(emailadress);     u.setfirstname(firstname);     u.setlastname(lastname);     system.out.println(em + ": adding course " + u);     em.persist(u);     em.flush();     system.out.println(u.getuser_id());     return u; }  public void addstudent(users2 u2) {     student s = new student();      s.setuser_id(u2.getuser_id());     s.setusername(u2.getusername());     s.setlastname(u2.getlastname());     s.setfirstname(u2.getfirstname());     s.setpassword(u2.getpassword());     s.setemailaddress((u2.getemailaddress()));      em.persist(s); } 

my method in jsf bean looks this:

@inject dbstore store;  public string createuser(){     long usrid;      string username = this.username;     string password = this.password;     string emailadress = this.emailaddress;     string firstname = this.firstname;     string lastname = this.lastname;     users2 u1 = store.adduser(username, password, emailadress, firstname, lastname);     //system.out.println(usrid);     string role = this.role;     if(this.role.equals("student"))         store.addstudent(u1);     return "admin_listcourses.xhtml";  } 

my entities:

import javax.persistence.id; import javax.validation.constraints.max; import java.util.list; import java.util.set;   @entity @table(name = "student") @secondarytable(name = "users2",     pkjoincolumns=@primarykeyjoincolumn(name="user_id", referencedcolumnname="user_id")) public class student {      /**      * created elev1 on 2016-08-25.      *      */     @id     @sequencegenerator(name="student_student_id_seq",         sequencename="student_student_id_seq",         allocationsize=1)     @generatedvalue(strategy = generationtype.sequence,         generator="seq")     @column(name = "student_id", updatable=false)     private long student_id;      @column(table="users2", name="username")     private string username;      @column(table="users2", name="firstname")     private string firstname;      @column(table="users2", name="lastname")     private string lastname;      @column(table="users2", name="password")     private string password;      @column(table="users2", name="emailaddress")     private string emailaddress;      @column(table="users2", name="user_id")      private  long user_id;      @manytomany     @jointable(name="student_course",         joincolumns=         @joincolumn(name="student_id", referencedcolumnname="student_id"),         inversejoincolumns=         @joincolumn(name="course_id", referencedcolumnname="course_id")     )       // public list<course> getcourses() { return courses ; }     public list<course> courses;       //getters , setters     public long getstudent_id() {         return student_id;     }      public void setstudent_id(long student_id) {         this.student_id = student_id;     }      public string getusername() {         return username;     }      public void setusername(string username) {         this.username = username;     }      public string getfirstname() {         return firstname;     }      public void setfirstname(string firstname) {         this.firstname = firstname;     }      public string getlastname() {         return lastname;     }      public void setlastname(string lastname) {         this.lastname = lastname;     }      public list<course> getcourses() {         return courses;     }      public void setcourses(list<course> courses) {         this.courses = courses;     }      public long getuser_id() {         return user_id;     }      public void setuser_id(long user_id) {         this.user_id = user_id;     }      public string getpassword() {         return password;     }      public void setpassword(string password) {         this.password = password;     }      public string getemailaddress() {         return emailaddress;     }      public void setemailaddress(string emailaddress) {         this.emailaddress = emailaddress;     } }  package se.lexicon.entities;  import javax.persistence.*; import java.io.serializable; import java.util.date;  @entity  public class users2{      // ***********************     // **     attributes    **     // ***********************      @id     @sequencegenerator(name="users_user_id_seq",         sequencename="users_user_id_seq",         allocationsize=1)     @generatedvalue(strategy = generationtype.sequence,         generator="seq")     private long user_id;      @column(name = "username", length = 64)     private string username;      private string password;     @column(name = "emailaddress", length = 64)     private string emailaddress;      @column(name = "firstname")     private string firstname;      @column(name = "lastname")     private string lastname;      @temporal(temporaltype.timestamp)     private date last_login;      // ************************     // **     constructors   **     // ************************      //  public user() {      //     public user(long user_id) {     //          this.user_id = user_id;     //      }      //      public user(long user_id, string username, string password, string emailaddress, ??? last_login) {      //          this.user_id = user_id;     //          this.username = username;     //         this.password = password;     //         this.emailaddress = emailaddress;     //        this.last_login = last_login;     //}      // ******************************     // **    getters & setters     **     // ******************************      public long getuser_id() {         return user_id;     }      public void setuser_id(long user_id) {         this.user_id = user_id;     }      public string getusername() {         return username;     }      public void setusername(string username) {         this.username = username;     }      public string getpassword() {         return password;     }      public void setpassword(string password) {         this.password = password;     }      public string getemailaddress() {         return emailaddress;     }      public void setemailaddress(string emailaddress) {         this.emailaddress = emailaddress;     }      public date getlast_login() {         return last_login;     }      public void setlast_login(date last_login) {         this.last_login = last_login;     }      public string getfirstname() {         return firstname;     }      public void setfirstname(string firstname) {         this.firstname = firstname;     }      public string getlastname() {         return lastname;     }      public void setlastname(string lastname) {         this.lastname = lastname;     } //}  } 

edit: solution. removed foregin_key constraint in student table. kept student_id, course_id , user_id in student class. removed connections between student , users2 in student class, instead use methods users2 class if student_id given. when user created, student, student added user_id of user set user_id of student.

now isn't solution, if 1 can solve original problem happy accept solution. solution have do.

when persist student instance jpa create row in users2 table user_id foreign key row in student table. there row same id in users2, persisted users2 instance before. that's reason, why facing sqlexception.

to me not make sense use secondarytable-approach here, there might me users no students @ all, right? in current model users table storing foreign key student table..

in case here seems more appropriate use inheritance (or maybe composition) here, instaead of secondarytable-approach. student is-a user, student may inherit user. find first overview how define mapping in case here:
https://en.wikibooks.org/wiki/java_persistence/inheritance


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -