现在,我正在学习hibernate,并开始在我的项目中使用它。这是一个CRUD应用程序。 我使用hibernate进行所有的crud操作。它适用于所有人。但是,One-To-Many&多对一,我厌倦了尝试它。最后它给了我以下错误。
org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]
然后我又经历了video tutorial。一开始对我来说非常简单。但是,我不能让它发挥作用。现在也是,
org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]
我在互联网上运行了一些搜索,有人告诉its a bug in Hibernate,有些人说,通过添加@GenereatedValue此错误将被清除。但是,nothings对我有用,
我希望我能得到一些解决方法!!
谢谢!
这是我的代码:
College.java
@Entity
public class College {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int collegeId;
private String collegeName;
private List<Student> students;
@OneToMany(targetEntity=Student.class, mappedBy="college", fetch=FetchType.EAGER)
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}//Other gettters & setters omitted
Student.java
@Entity
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int studentId;
private String studentName;
private College college;
@ManyToOne
@JoinColumn(name="collegeId")
public College getCollege() {
return college;
}
public void setCollege(College college) {
this.college = college;
}//Other gettters & setters omitted
Main.java:
public class Main {
private static org.hibernate.SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
initSessionFactory();
}
return sessionFactory;
}
private static synchronized void initSessionFactory() {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
public static Session getSession() {
return getSessionFactory().openSession();
}
public static void main (String[] args) {
Session session = getSession();
Transaction transaction = session.beginTransaction();
College college = new College();
college.setCollegeName("Dr.MCET");
Student student1 = new Student();
student1.setStudentName("Peter");
Student student2 = new Student();
student2.setStudentName("John");
student1.setCollege(college);
student2.setCollege(college);
session.save(student1);
session.save(student2);
transaction.commit();
}
}
控制台:
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290)
at org.hibernate.mapping.Property.isValid(Property.java:217)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:463)
at org.hibernate.mapping.RootClass.validate(RootClass.java:235)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1330)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1833)
at test.hibernate.Main.initSessionFactory(Main.java:22)
at test.hibernate.Main.getSessionFactory(Main.java:16)
at test.hibernate.Main.getSession(Main.java:27)
at test.hibernate.Main.main(Main.java:43)
XML:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/dummy</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="test.hibernate.Student" />
<mapping class="test.hibernate.College" />
</session-factory>
答案 0 :(得分:137)
您正在使用字段访问策略(由@Id注释确定)。将任何与JPA相关的注释放在每个字段的正上方而不是getter属性
@OneToMany(targetEntity=Student.class, mappedBy="college", fetch=FetchType.EAGER)
private List<Student> students;
答案 1 :(得分:54)
将@ElementCollection
添加到“列表”字段可解决此问题:
@Column
@ElementCollection(targetClass=Integer.class)
private List<Integer> countries;
答案 2 :(得分:21)
访问策略
的问题作为JPA提供者,Hibernate可以自省两个实体属性 (实例字段)或访问者(实例属性)。默认情况下,
@Id
注释的位置提供默认访问权限 战略。放置在字段上时,Hibernate将假设基于字段 访问。放置在标识符getter上,Hibernate将使用 基于财产的访问。
基于字段的访问
使用基于字段的访问时,添加其他实体级方法要灵活得多,因为Hibernate不会考虑持久化状态的那些部分
@Entity
public class Simple {
@Id
private Integer id;
@OneToMany(targetEntity=Student.class, mappedBy="college",
fetch=FetchType.EAGER)
private List<Student> students;
//getter +setter
}
基于财产的访问
当使用基于属性的访问时,Hibernate使用访问器来读取和写入实体状态
@Entity
public class Simple {
private Integer id;
private List<Student> students;
@Id
public Integer getId() {
return id;
}
public void setId( Integer id ) {
this.id = id;
}
@OneToMany(targetEntity=Student.class, mappedBy="college",
fetch=FetchType.EAGER)
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
但您不能同时使用基于字段和基于属性的访问。它将为您显示该错误
如需了解更多信息,请按照this
进行操作答案 3 :(得分:6)
@Access(AccessType.PROPERTY)
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="userId")
public User getUser() {
return user;
}
我有同样的问题,我通过添加@Access(AccessType.PROPERTY)
答案 4 :(得分:2)
在我的情况下,它愚蠢地缺少了@OneToOne批注,我没有设置@MapsId
答案 5 :(得分:0)
别担心!出现此问题是由于注释。基于属性的访问不是基于字段的访问,而是解决了这个问题。代码如下:
package onetomanymapping;
import java.util.List;
import javax.persistence.*;
@Entity
public class College {
private int collegeId;
private String collegeName;
private List<Student> students;
@OneToMany(targetEntity = Student.class, mappedBy = "college",
cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Id
@GeneratedValue
public int getCollegeId() {
return collegeId;
}
public void setCollegeId(int collegeId) {
this.collegeId = collegeId;
}
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
}
答案 6 :(得分:0)
虽然我是冬眠的新手,但研究很少(可以说是反复试验) 我发现这是由于注释方法/文件不一致。
当您在变量上注释@ID时,请确保所有其他注释也仅在变量上完成 当您在getter方法上进行注释时,请确保仅对所有其他getter方法进行注释,而不是对它们各自的变量进行注释。
答案 7 :(得分:0)
万一其他人因我遇到的同样问题而降落在这里。我收到与上述相同的错误:
调用init方法失败;嵌套异常为 org.hibernate.MappingException:无法确定以下类型: java.util.Collection,在表中:
Hibernate使用反射来确定实体中的列。我有一个私有方法,该方法以“ get”开头,并返回一个也是休眠实体的对象。甚至您想要休眠忽略的私有获取器也必须使用@Transient进行注释。添加@Transient批注后,一切正常。
@Transient
private List<AHibernateEntity> getHibernateEntities() {
....
}
答案 8 :(得分:0)
只需在数组列表变量上插入@ElementCollection批注,如下所示:
@ElementCollection
private List<Price> prices = new ArrayList<Price>();
我希望这对您有帮助
答案 9 :(得分:0)
将架构名称添加到实体,它将找到它。为我工作!
答案 10 :(得分:0)
列出的解决方案都不适合我,原因如下:我将实体继承与属性访问策略结合使用。
@Entity(name = "spec")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula(value = "type")
public abstract class Spec extends BasicEntity { ... }
两个继承者有一个在超类中没有的属性。继承者之一具有 JoinColumn
和 OneToOne
注释以整合 Basic
属性类型用法(实际上,Intellij IDEA 会突出显示该字段,如果它没有带有 JoinColumn
注释的适当 getter)。< /p>
@Data
@Entity
@DiscriminatorValue("data")
public class DataSpec extends Spec {
private Payload payload;
@OneToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "payload_id")
public Payload getPayload() {
return payload;
}
然而,第二个继承者没有这个新属性的任何注释,而且 IDEA 以某种方式没有突出显示它来警告我这个问题!
@Data
@Entity
@DiscriminatorValue("blob")
public class BlobSpec extends Spec {
private Payload payload;
//^^^^^^^^^^^^^^^^^^^^^^^^ No getter with @JoinColumn! Should be highlighted with static code check!
我只能通过调试休眠类来对问题进行分类。我发现我的实体在 MetadataImpl.validate
方法中没有通过验证。例外不会告诉你这件事。它只打印 spec
表中发生的错误,不是详细信息。
答案 11 :(得分:-2)
有同样的问题,我的问题是我没有在 id 字段上指定 @Id 注释。
当我注释它时,一切都进行得很顺利。