org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]

I'm using Hibernate for all CRUD operations in my project. It doesn't work for One-To-Many and Many-To-One relationships. It gives me the below error.

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]

Then again i went through this video tutorial. It is very simple to me, in the beginning. But, i cant make it work. It also now, says

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]

I have ran some searches in the internet, there someone telling its a bug in Hibernate, and some says, by adding @GenereatedValue this error ll be cleared, but it doesn't work for me.

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




}

Console:

 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)

The 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>

304060 次浏览

您正在使用 现场存取策略现场存取策略(由@Id 注释确定)。将任何与 JPA 相关的注释放在每个字段的正上方,而不是 getter 属性

@OneToMany(targetEntity=Student.class, mappedBy="college", fetch=FetchType.EAGER)
private List<Student> students;

@ElementCollection添加到 List 字段解决了这个问题:

    @Column
@ElementCollection(targetClass=Integer.class)
private List<Integer> countries;
@Access(AccessType.PROPERTY)
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="userId")
public User getUser() {
return user;
}

我有相同的问题,我解决了它加上 @Access(AccessType.PROPERTY)

别担心!出现此问题是因为注释。基于属性的访问解决了这个问题,而不是基于字段的访问。守则如下:

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

}

访问策略的问题

作为 JPA 提供程序,Hibernate 可以同时检查两个实体属性 (实例字段)或访问器(实例属性), @Id注释的位置提供了默认访问权限 当放置在字段上时,Hibernate 将假设基于字段的 放在标识符 getter 上,Hibernate 将使用 基于属性的访问。

基于现场的访问

When using field-based access, adding other entity-level methods is much more flexible because Hibernate won’t consider those part of the persistence state

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


}

但是不能同时使用基于字段和基于属性的访问。它会为你显示这样的错误

想要了解更多的想法,请跟随 这个

Though I am new to hibernate but with little research (trial and error we can say) I found out that it is due to inconsistency in annotating the methods/fileds.

when you are annotating @ID on variable make sure all other annotations are also done on variable only and when you are annotating it on getter method same make sure you are annotating all other getter methods only and not their respective variables.

如果其他人在这里遇到同样的问题,我也会得到同样的错误:

Invocation of init method failed; nested exception is 异常: 无法确定类型: 收集,在桌子上:

Hibernate 使用反射来确定实体中的哪些列。我有一个以“ get”开头的私有方法,它返回一个也是休眠实体的对象。即使是您希望休眠时忽略的私有 getter,也必须使用@Tranent 进行注释。在添加了@瞬态注释之后,一切都正常了。

@Transient
private List<AHibernateEntity> getHibernateEntities() {
....
}

只需在数组列表变量上插入@ElementCollection 注释,如下所示:

@ElementCollection
private List<Price> prices = new ArrayList<Price>();

希望这个能帮上忙

In my case it was stupid missing of @OneToOne annotation, i set @MapsId without it

将模式名添加到实体中,它就会找到它!

有同样的问题,我的问题是我没有在 id 字段上指定@Id 注释。

当我注释的时候,一切都很顺利。

上面列出的两种解决方案都不适合我,原因如下: 我使用实体继承和属性访问策略。

@Entity(name = "spec")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula(value = "type")
public abstract class Spec extends BasicEntity { ... }

两个继承者具有超类中未显示的属性。其中一个继承者使用 JoinColumnOneToOne注释来整合 Basic属性类型的使用(实际上,如果 Intellij IDEA 没有使用适当的 getter 和 JoinColumn注释,它会突出显示该字段)。

@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!

我只能通过调试 hibernate 类来分流这个问题。我发现我的实体没有通过 MetadataImpl.validate方法的验证。异常不会告诉你。它只打印在 spec表中发生的错误,这不是一个详细的消息。

Just remove

MappedBy = “ college” in@OneTomany (targetEntity = Student.class,mappedBy = “ college”, Get = FetchType.EAGER)

这解决了我的问题