Mapping Value in Junction Table & hibernate.PropertyAccessException: could not set a field value by reflection setter

匿名 (未验证) 提交于 2019-12-03 00:57:01

问题:

First post to stackoverflow, so please excuse if I did not post correctly. I posted a follow-up question with code on an old thread Mapping value in junction table to Entity as I am not able to get the recommended solution to function properly. I am using OpenXava and receive error "Impossible to execute Save action: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of org.openxava.invoicing.model.CourseAssignmentId.course". Any help is appreciated. My code:

User Class:

@Entity @Table(name="users") public class User {     @Id     @Column(name="pk1")     private Long id;      public Long getid() {         return id;     }      public void setid(Long id) {         this.id = id;     }      @Column(name="user_id")     private String userID;      public String getuserID(){         return userID;     }      public void setuserID(String userID) {         this.userID = userID;     }      @OneToMany(mappedBy="user")     private Collection<CourseAssignment> courseAssignments;      public Collection<CourseAssignment> getcourseAssignments() {         return courseAssignments;     }      public void setcourseAssignments(Collection<CourseAssignment> courseAssignments) {         this.courseAssignments = courseAssignments;     }  } 

Course Class:

@Entity @Table(name="courses") public class Course {      @Id     @Column(name="pk1")     private Long id;      public Long getid() {         return id;     }      public void setid(Long id) {         this.id = id;     }      @Column(name="course_name")     private String name;      public String getname() {         return name;     }      public void setname(String name) {         this.name = name;     }         @OneToMany(mappedBy = "course")     private Collection<CourseAssignment> courseAssignments;      public Collection<CourseAssignment> getcourseAssignments() {         return courseAssignments;     }      public void setcourseAssignments(Collection<CourseAssignment> courseAssignments) {         this.courseAssignments = courseAssignments;     }   } 

CourseAssignment Class:

@Entity @Table(name="course_users") @IdClass(CourseAssignmentId.class) public class CourseAssignment {      @Id     @ManyToOne     @JoinColumn(name="user_pk1")     private User user;      public User getuser() {         return user;     }      public void setuser(User user) {         this.user = user;     }      @Id     @ManyToOne     @JoinColumn(name="crsmain_pk1")     private Course course;      public Course getcourse() {         return course;     }      public void setcourse(Course course) {         this.course = course;     }      @Column(name="role")     private String role;      public String getrole() {         return role;     }      public void setrole(String role) {         this.role = role;     } } 

CourseAssignmentId Class:

@Embeddable public class CourseAssignmentId implements java.io.Serializable {      private static final long serialVersionUID = 1L;       @Column(name="user_pk1")     private Long user;      public Long getuser() {         return user;     }      public void setuser(Long user) {         this.user = user;     }      @Column(name="crsmain_pk1")     private Long course;      public Long getcourse() {         return course;     }      public void setcourse(Long course) {         this.course = course;     }    } 

回答1:

Some things to try:

  • Removing the @Embeddable annotation from CourseAssignmentId (I don't think it is appropriate in this context)
  • Removing the @Column annotations from CourseAssignmentId
  • Implementing equals() and hashCode() in CourseAssignmentId


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!