gpt4 book ai didi

java - 连接表和 hibernate.PropertyAccessException : could not set a field value by reflection setter 中的映射值

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:53 25 4
gpt4 key购买 nike

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

用户类别:

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

}

类(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;
}


}

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

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

最佳答案

一些尝试:

  • CourseAssignmentId 中删除 @Embeddable 注释(我认为在这种情况下不合适)
  • CourseAssignmentId 中删除 @Column 注释
  • CourseAssignmentId 中实现 equals()hashCode()

关于java - 连接表和 hibernate.PropertyAccessException : could not set a field value by reflection setter 中的映射值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18807321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com