![](/img/trans.png)
[英]org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user, for columns: [org.hibernate.mapping.Column(events)]
[英]org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]
我正在將 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: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]
我在互聯網上進行了一些搜索,有人告訴它 Hibernate 中的一個錯誤,有人說,通過添加@GenereatedValue這個錯誤將被清除,但它對我不起作用。
大學.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
學生.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
主.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>
您正在使用字段訪問策略(由 @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;
訪問策略的問題
作為 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;
}
}
但是您不能同時使用基於字段和基於屬性的訪問。 它會為你顯示類似的錯誤
有關更多想法,請遵循此
@Access(AccessType.PROPERTY)
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="userId")
public User getUser() {
return user;
}
我有同樣的問題,我通過添加@Access(AccessType.PROPERTY)
解決了它
只需在數組列表變量上插入 @ElementCollection 注釋,如下所示:
@ElementCollection
private List<Price> prices = new ArrayList<Price>();
我希望這有幫助
雖然我是 hibernate 的新手,但經過很少的研究(我們可以說是反復試驗),我發現這是由於注釋方法/文件的不一致造成的。
當你在變量上注釋@ID 時,確保所有其他注釋也只對變量進行,當你在 getter 方法上注釋它時,確保你只注釋所有其他 getter 方法而不是它們各自的變量。
在我的情況下,@OneToOne 注釋的缺失是愚蠢的,我設置了 @MapsId 沒有它
別擔心! 出現此問題的原因是注釋。 代替基於字段的訪問,基於屬性的訪問解決了這個問題。 代碼如下:
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;
}
}
萬一其他人遇到我遇到的同樣問題。 我遇到了與上面相同的錯誤:
init 方法調用失敗; 嵌套異常是 org.hibernate.MappingException:無法確定類型:java.util.Collection,在表中:
Hibernate 使用反射來確定實體中的哪些列。 我有一個以“get”開頭的私有方法,並返回一個也是休眠實體的對象。 即使您希望休眠忽略的私有 getter 也必須使用 @Transient 進行注釋。 添加 @Transient 注釋后,一切正常。
@Transient
private List<AHibernateEntity> getHibernateEntities() {
....
}
將架構名稱添加到實體,它會找到它。 為我工作!
列出的解決方案都不適合我,原因如下:我將實體繼承與屬性訪問策略一起使用。
@Entity(name = "spec")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula(value = "type")
public abstract class Spec extends BasicEntity { ... }
兩個繼承者有一個在超類中沒有的屬性。 繼承者之一具有JoinColumn
和OneToOne
注釋以整合Basic
屬性類型用法(實際上,Intellij IDEA 會突出顯示該字段,如果它沒有合適的帶有JoinColumn
注釋的 getter)。
@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
表中發生的錯誤,而不是詳細消息。
只需刪除
的mappedBy = “大學”在@OneToMany(targetEntity = Student.class,的mappedBy = “大學”,取= FetchType.EAGER)
這解決了我的問題。 ;)
有同樣的問題,我的問題是我沒有在 id 字段上指定 @Id 注釋。
當我注釋它時,一切都運行得很順利。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.