java - JPA - Changing the Name of the mappedBy column -


okay have application maps semesters courses -:

public class course {      private string courseid;     private string coursename;      private collection<semester> semesters = new arraylist<>();        @onetomany (targetentity = semester.class, mappedby = "course")     public collection<semester> getsemesters() {         return semesters;     }     public void setsemesters(collection<semester> semesters) {         this.semesters = semesters;     }     @id     @column (name = "course_id")     public string getcourseid() {         return courseid;     }     public void setcourseid(string courseid) {         this.courseid = courseid;     }     @column (name = "course_name", nullable = false, unique = true)     public string getcoursename() {         return coursename;     }     public void setcoursename(string coursename) {         this.coursename = coursename;     }     } 

as can see users class mapped semesters entity using 1 many mapping.

the semester class follows - :

@entity @table (name = "semesters") public class semester {      private int semnum;      private course course;      @manytoone     public course getcourse() {         return course;     }      public void setcourse(course course) {         this.course = course;     }      @id     @column (name = "sem_num")     public int getsemnum() {         return semnum;     }      public void setsemnum(int semnum) {         this.semnum = semnum;     }     } 

as can see using mappedby map course directly semester table. problem field in semester table comes course_course_id.

how can change column name course_id ?

you need use @joincolumn annotation.

@manytoone @joincolumn(name="course_id") public course getcourse() {     //code } 

i suggest read documentation explained in there. if don't want read docs, ready long , painful journey, jpa not simple technology, , has multitude of gotchas.


Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -