java - Composite primary key with many to one relation ship does not support mapping error -
i have 3 entities form association relationship having composite primary keys foreign key of other tables. these implementation of these entites:
@entity @table(name = "studentscourses", schema = "gpa") @namedquery(name = "getallstdcrs", query = "select sc studentscourses sc") @idclass(studentcourseid.class) public class studentscourses implements serializable { private static final long serialversionuid = 1l; @id @column(name="studentid", insertable=false, updatable= false) private int studentid; @id @column(name="crsid", insertable=false, updatable=false) private int crsid; @manytoone @joincolumn(name="studentid") student student; @manytoone @joincolumn(name="crsid") course course; public studentscourses() { super(); } public void setstudentid(int studentid) { this.studentid = studentid; } public int getstudentid() { return studentid; } public void setcrsid(int crsid) { this.crsid = crsid; } public int getcrsid() { return crsid; } }
course entity:
@entity @table(name = "courses", schema = "gpa") @namedquery(name = "getallcourses", query = "select c course c") public class course implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue(strategy=generationtype.identity) @column(name="crsid") private int crsid; private string name; @onetomany(mappedby="course", fetch=fetchtype.lazy) private set<assesment> assesments; @onetomany(mappedby="course", fetch=fetchtype.lazy) private set<studentscourses> studentcourses; public course() { super(); } public int getcrsid() { return crsid; } public void setcrsid(int crsid) { this.crsid = crsid; } public string getname() { return name; } public void setname(string name) { this.name = name; } public void setassesments(set<assesment> assesments) { this.assesments = assesments; } public set<assesment> getassesments() { return assesments; } public void setstudentcourses(set<studentscourses> studentcourses) { this.studentcourses = studentcourses; } public set<studentscourses> getstudentcourses() { return studentcourses; } }
student entity:
@entity @table(name = "students", schema = "gpa") @namedquery(name = "getallstudents", query = "select s student s") public class student implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue(strategy=generationtype.identity) private int studentid; private string studentname; @onetomany(mappedby="student", fetch = fetchtype.lazy) private set<assesment> assesments; @onetomany(mappedby="student", fetch = fetchtype.lazy) private set<studentscourses> studentcourses; public student() { super(); } public int getstudnetid() { return studentid; } public void setstudnetid(int stdid) { this.studentid = stdid; } public void setstudentname(string studentname) { this.studentname = studentname; } public string getstudentname() { return studentname; } public void setassesments(set<assesment> assesments) { this.assesments = assesments; } public set<assesment> getassesments() { return assesments; } public void setstudentcourses(set<studentscourses> studentcourses) { this.studentcourses = studentcourses; } public set<studentscourses> getstudentcourses() { return studentcourses; } }
when try lanuch program getting following error:
[4/5/13 0:30:15:243 edt] 00000028 webapp e com.ibm.ws.webcontainer.webapp.webapp logservleterror srve0293e: [servlet error]-[com.gpa.app.servlet.loginservlet]: <openjpa-2.1.1-snapshot-r422266:1141200 fatal user error> org.apache.openjpa.persistence.argumentexception: field "com.gpa.app.entities.course.studentcourses" cannot declare mapped field. mapping strategy (org.apache.openjpa.jdbc.meta.strats.handlerfieldstrategy) not support mapping field.
what cause of prob. appreciate help.
so problem in @id classes. in class didn't implemented equal , hash code method , didn't implemented serializable. after done worked smoothly. eelke, gave me idea try in different version , helped me find out cause.
my modified code in looks this:
public class studentcourseid implements serializable{ private static final long serialversionuid = 1l; private int studentid; private int crsid; public studentcourseid() { } public studentcourseid(int studentid, int crsid) { this.studentid = studentid; this.crsid = crsid; } public void setstudentid(int studentid) { this.studentid = studentid; } public int getstudentid() { return studentid; } public void setcrsid(int crsid) { this.crsid = crsid; } public int getcrsid() { return crsid; } public int hashcode() { return studentid + crsid; } public boolean equals(object o) { return ((o instanceof studentcourseid) && studentid == ((studentcourseid) o).getstudentid() && crsid == ((studentcourseid) o) .getcrsid()); } } public int getcrsid() { return crsid; } }
hope helps in future.
thanks, sas
Comments
Post a Comment