休眠错误:无法确定 java.util.List 的类型
问题:
尝试使用Hibernate进行涉及一对多和多对一关系的CRUD操作时,遇到以下错误:
org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]
实体类:
<code class="java">@Entity public class College { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int collegeId; private String collegeName; @OneToMany(targetEntity = Student.class, mappedBy = "college", fetch = FetchType.EAGER) private List<Student> students; } @Entity public class Student { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int studentId; private String studentName; @ManyToOne @JoinColumn(name = "collegeId") private College college; }</code>
其他详细信息:
解决方案:
出现此问题是因为 Hibernate 无法确定由于使用字段访问策略而导致的学生字段类型。要解决此问题,JPA 注释应直接放置在每个字段上方,而不是 getter 属性上方。
<code class="java">@Entity public class College { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int collegeId; private String collegeName; @OneToMany(targetEntity = Student.class, mappedBy = "college", fetch = FetchType.EAGER) public List<Student> students; }</code>
以上是为什么 Hibernate 在一对多关系中抛出'无法确定 java.util.List 的类型”错误?的详细内容。更多信息请关注PHP中文网其他相关文章!