连接表中的映射值& hibernate.PropertyAccessException:无法通过反射设置器设置字段值

时间:2013-09-14 23:15:28

标签: java hibernate openxava

首先发布到stackoverflow,所以请原谅我没有正确发布。我在旧线程Mapping value in junction table to Entity上发布了一个带代码的后续问题,因为我无法获得推荐的解决方案以正常运行。我正在使用OpenXava并收到错误"无法执行保存操作:org.hibernate.PropertyAccessException:无法通过org.openxava.invoicing.model.CourseAssignmentId.course"的反射设置器设置字段值。任何帮助表示赞赏。我的代码:

用户类:

@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;
    }

}

课程班级:

@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类:

@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类:

@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 个答案:

答案 0 :(得分:1)

有些事情要尝试:

  • @Embeddable中移除CourseAssignmentId注释(我不认为这在此上下文中是合适的)
  • @Column
  • 中删除CourseAssignmentId注释
  • equals()
  • 中实施hashCode()CourseAssignmentId