org.hibernate。MappingException:无法为:java.util确定类型。列表,在表:学院,为专栏:[org.hibernate.mapp . column(学生)]

时间:2022-01-28 19:44:28

Now, i am learning hibernate, and started to using it in my project. It is a CRUD application. I used hibernate for all the crud operations. It works for all of them. But, the One-To-Many & Many-To-One, i am tired of trying it. Finally it gives me the below error.

现在,我正在学习hibernate,并开始在我的项目中使用它。这是一个粗略的应用。我使用hibernate进行所有crud操作。它适用于所有人。但是,一对多和多对一,我厌倦了尝试它。最后给出了下面的错误。

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

org.hibernate。MappingException:无法为:java.util确定类型。列表,在表:学院,为专栏:[org.hibernate.mapp . column(学生)]

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)]

org.hibernate。MappingException:无法为:java.util确定类型。列表,在表:学院,为专栏:[org.hibernate.mapp . column(学生)]

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, nothings works for me,

我在互联网上搜索过,有人告诉它在Hibernate中有一个bug,有人说,通过添加@GenereatedValue这个错误将被清除。但是,没有什么对我有用,

I hope i ll get some fix!!

我希望我能修一下。

Thanks!

谢谢!

Here my Code:

这里我的代码:

College.java

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

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:

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:

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

5 个解决方案

#1


118  

You are using field access strategy (determined by @Id annotation). Put any JPA related annotation right above each field instead of getter property

您正在使用字段访问策略(由@Id注释决定)。将任何JPA相关的注释放在每个字段之上,而不是getter属性。

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

#2


39  

Adding the @ElementCollection to the List field solved this issue:

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

    @Column
    @ElementCollection(targetClass=Integer.class)
    private List<Integer> countries;

#3


13  

Problem with Access strategies

访问策略的问题

As a JPA provider, Hibernate can introspect both the entity attributes (instance fields) or the accessors (instance properties). By default, the placement of the @Id annotation gives the default access strategy. When placed on a field, Hibernate will assume field-based access. Placed on the identifier getter, Hibernate will use property-based access.

作为一个JPA提供者,Hibernate可以检查实体属性(实例字段)或访问器(实例属性)。默认情况下,@Id注释的位置提供了默认的访问策略。当放置在一个字段上时,Hibernate将假定基于字段的访问。将其放在标识符getter上,Hibernate将使用基于属性的访问。

Field-based access

实地访问

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

在使用基于字段的访问时,添加其他实体级方法要灵活得多,因为Hibernate不会考虑持久性状态的那些部分

@Entity
public class Simple {

@Id
private Integer id;

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

//getter +setter
}

Property-based access

基于属性的访问

When using property-based access, Hibernate uses the accessors for both reading and writing the entity state

当使用基于属性的访问时,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;
}

}

But you can't use both Field-based and Property-based access at the same time. It will show like that error for you

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

For more idea follow this

想知道更多的想法。

#4


5  

@Access(AccessType.PROPERTY)
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="userId")
public User getUser() {
    return user;
}

I have the same problems, I solved it by add @Access(AccessType.PROPERTY)

我有同样的问题,我通过添加@Access(AccessType.PROPERTY)解决了它

#5


1  

Don't worry! This problem occurs because of the annotation. Instead of Field based access, Property based access solves this problem. The code as follows:

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

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

}

}

#1


118  

You are using field access strategy (determined by @Id annotation). Put any JPA related annotation right above each field instead of getter property

您正在使用字段访问策略(由@Id注释决定)。将任何JPA相关的注释放在每个字段之上,而不是getter属性。

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

#2


39  

Adding the @ElementCollection to the List field solved this issue:

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

    @Column
    @ElementCollection(targetClass=Integer.class)
    private List<Integer> countries;

#3


13  

Problem with Access strategies

访问策略的问题

As a JPA provider, Hibernate can introspect both the entity attributes (instance fields) or the accessors (instance properties). By default, the placement of the @Id annotation gives the default access strategy. When placed on a field, Hibernate will assume field-based access. Placed on the identifier getter, Hibernate will use property-based access.

作为一个JPA提供者,Hibernate可以检查实体属性(实例字段)或访问器(实例属性)。默认情况下,@Id注释的位置提供了默认的访问策略。当放置在一个字段上时,Hibernate将假定基于字段的访问。将其放在标识符getter上,Hibernate将使用基于属性的访问。

Field-based access

实地访问

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

在使用基于字段的访问时,添加其他实体级方法要灵活得多,因为Hibernate不会考虑持久性状态的那些部分

@Entity
public class Simple {

@Id
private Integer id;

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

//getter +setter
}

Property-based access

基于属性的访问

When using property-based access, Hibernate uses the accessors for both reading and writing the entity state

当使用基于属性的访问时,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;
}

}

But you can't use both Field-based and Property-based access at the same time. It will show like that error for you

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

For more idea follow this

想知道更多的想法。

#4


5  

@Access(AccessType.PROPERTY)
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="userId")
public User getUser() {
    return user;
}

I have the same problems, I solved it by add @Access(AccessType.PROPERTY)

我有同样的问题,我通过添加@Access(AccessType.PROPERTY)解决了它

#5


1  

Don't worry! This problem occurs because of the annotation. Instead of Field based access, Property based access solves this problem. The code as follows:

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

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

}

}