org.hibernate.MappingException:无法确定:java.util.List的类型,在表:College,对于列:[org.hibernate.mapping.Column(students)]

82 java orm hibernate

现在,我正在学习hibernate,并开始在我的项目中使用它.这是一个CRUD应用程序.我使用hibernate进行所有的crud操作.它适用于所有人.但是,One-To-Many和Many-To-One,我厌倦了尝试它.最后它给了我以下错误.

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

然后我再次浏览了这个视频教程.一开始对我来说非常简单.但是,我不能让它发挥作用.现在,它说

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

我在互联网上运行了一些搜索,有人告诉它在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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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


}
Run Code Online (Sandbox Code Playgroud)

安慰:

 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)
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Art*_*ald 146

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

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

  • @Arthur +1好捕捉@MaRaVan你可以使用字段访问策略或属性访问策略但是你必须在类层次结构中保持一致,你不能混合策略,至少不能在JPA 1.0中.JPA 2.0虽然可以混合策略,但是你需要额外的注释(因此更复杂).因此,建议选择一种策略并在您的应用程序中坚持使用它.见[2.2.2.2.访问类型](http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e535). (7认同)

Big*_*va2 67

添加@ElementCollection到列表字段解决了此问题:

    @Column
    @ElementCollection(targetClass=Integer.class)
    private List<Integer> countries;
Run Code Online (Sandbox Code Playgroud)

  • 当你提到列表`<Integer>`的类型时,没有必要使用+`targetClass` (3认同)

Jus*_*ode 28

访问策略的问题

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

基于现场的访问

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

@Entity
public class Simple {

@Id
private Integer id;

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

//getter +setter
}
Run Code Online (Sandbox Code Playgroud)

基于财产的访问

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

}
Run Code Online (Sandbox Code Playgroud)

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

更多想法请关注此事


Ren*_*los 13

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

@ElementCollection
private List<Price> prices = new ArrayList<Price>();
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助


小智 7

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

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