休眠映射异常:无法确定列的类型
Posted
技术标签:
【中文标题】休眠映射异常:无法确定列的类型【英文标题】:Hibernate mapping exception: Could not determine type for columns 【发布时间】:2015-07-05 18:17:40 【问题描述】:我想在我的两个类 Customer 和 CustomerGroup 之间做一个映射,我收到了这个错误:
Exception in thread "AWT-EventQueue-0" org.hibernate.MappingException: Could not determine type for: studyproject.CustomerGroup, at table: CUSTOMER, for columns: [org.hibernate.mapping.Column(assignedTo)]
我的 CustomerGroup 类是:(仅供参考:一开始我不需要 BonusPackage 类型,因此我没有对其进行注释)
package studyproject;
import javax.persistence.Column;
import javax.persistence.Id;
public class CustomerGroup
private String name;
private String description;
private int id;
private BonusPackage bonusPackage;
public CustomerGroup(int id,String name, String description,
BonusPackage bonusPackage)
super();
this.id=id;
this.name = name;
this.description = description;
this.bonusPackage = bonusPackage;
@Column(name="id")
public int getId()
return id;
public void setId(int id)
this.id = id;
@Column(name="name")
public String getName()
return name;
public void setName(String name)
this.name = name;
@Column(name="description")
public String getDescription()
return description;
public void setDescription(String description)
this.description = description;
@Transient
public BonusPackage getBonusPackage()
return bonusPackage;
public void setBonusPackage(BonusPackage bonusPackage)
this.bonusPackage = bonusPackage;
@Transient
public boolean generateServiceMails()
return false;
客户类:
@Entity
@Table(name="CUSTOMER")
public class Customer
private int id;
private String forename;
private String surname;
private char gender;
private Date birthday;
private double generatedProfitsTotal;
private double generatedProfitsLastYear;
private CustomerGroup assignedTo;
public Customer(int id, String forename, String surname, char gender,
Date birthday)
super();
this.id = id;
this.forename = forename;
this.surname = surname;
this.gender = gender;
this.birthday = birthday;
@Id
public int getId()
return id;
public void setId(int id)
this.id = id;
@Column(name = "forename")
public String getForename()
return forename;
public void setForename(String forename)
this.forename = forename;
@Column(name = "surename")
public String getSurname()
return surname;
public void setSurname(String surname)
this.surname = surname;
@Column(name = "gender")
public char getGender()
return gender;
public void setGender(char gender)
this.gender = gender;
@Column(name = "birthday")
public Date getBirthday()
return birthday;
public void setBirthday(Date birthday)
this.birthday = birthday;
@Column(name = "generatedProfitsTotal")
public double getGeneratedProfitsTotal()
return generatedProfitsTotal;
public void setGeneratedProfitsTotal(double generatedProfitsTotal)
this.generatedProfitsTotal = generatedProfitsTotal;
@Column(name = "generatedProfitsLastYear")
public double getGeneratedProfitsLastYear()
return generatedProfitsLastYear;
public void setGeneratedProfitsLastYear(double generatedProfitsLastYear)
this.generatedProfitsLastYear = generatedProfitsLastYear;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "id", cascade = CascadeType.ALL)
public CustomerGroup getAssignedTo()
return assignedTo;
public void setAssignedTo(CustomerGroup assignedTo)
this.assignedTo = assignedTo;
提交事务的方法:
public String saveAllDataORM()
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
for(Customer c:this.getCustomers())
session.persist(c);
transaction.commit();
session.close();
return "Done";
我的 customer.hbm.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="studyproject.Customer"
table="CUSTOMER">
<id name="id" type="int">
<column name="ID"/>
<generator class="assigned"/>
</id>
<property name="forename" type="java.lang.String">
<column name="FORENAME"/>
</property>
<property name="surname" type="java.lang.String">
<column name="SURNAME"/>
</property>
<property name="gender" type="java.lang.Character">
<column name="GENDER"/>
</property>
<property name="birthday" type="java.util.Date">
<column name="birthday"/>
</property>
property name="generatedProfitsTotal" type="java.lang.Double">
<column name="generatedProfitsTotal"/>
</property>
<property name="generatedProfitsLastYear" type="java.lang.Double">
<column name="generatedProfitsLastYear"/>
</property>
<property name = "assignedTo" type ="studyproject.CustomerGroup">
<column name="assignedTo" />
</property>
</class>
</hibernate-mapping>
我在 *** 上查看了几个常见问题解答和其他线程,主要是注释存在问题。我试图在我的代码中修复它,但我无法完成..:/
为了更好地说明,我在这里有一个 UML:
【问题讨论】:
为什么要混合注解映射和xml映射,不想映射的属性可以标记为@Transient。 实际上我之前尝试过使用休眠,而没有尝试映射类型“customerGroup”。然后在我扩展到“客户组”类型之后 - 它不起作用,所以我尝试映射它们。 【参考方案1】:假设类CustomerGroup
代表数据库中的CustomerGroup
表,添加下面提到的注释:
@Entity
@Table(name="CustomerGroup")
public class CustomerGroup
【讨论】:
仍然遇到同样的错误。也许错误在 Customer.hbm.xml 中?映射可能有错误。 您在这里混合了 xml 映射和注释。在您的情况下,仅注释就足够了。首先从休眠配置文件中删除customer.hbm.xml
。
在你的休眠配置文件中添加<mapping class="studyproject.Customer"/>
。
看看:***.com/questions/3189056/…
理想情况下你应该:)【参考方案2】:
您必须告诉 Hibernate 您想要在 Customer
和 CustomerGroup
之间建立哪种关联:一对一或多对一。
此外,默认情况下,所有属性都由 JPA 标准映射(这就是如果您使用 JPA 注释,Hibernate 必须遵守它的原因)。
如果你想让一个属性瞬态,你必须明确地这样做:
@Transient
public BonusPackage getBonusPackage()
return bonusPackage;
【讨论】:
仍然有相同的错误消息:线程“AWT-EventQueue-0”中的异常 org.hibernate.MappingException:无法确定类型:studyproject.CustomerGroup,在表:客户,列:[ org.hibernate.mapping.Column(assignedTo)] 请看我编辑的答案。抱歉,一开始我没有看到实际的错误。尽管如此,@Transient
的评论仍然适用。
我是否正确理解@OneToOne 关联我需要一个外键?事实并非如此。为了说明我的环境,我在这里上传了一个 UML:fs2.directupload.net/images/150705/2fahgyk2.jpg 通常情况下,CustomerGroup 在客户类中添加了一个方法。 (this.customerGroups.add(new CustomerGroup(......));
那么,你怎么知道一个客户属于哪个客户群呢?
好的,在这种情况下,也将getAssignedTo
设为瞬态,以便JPA 忽略它,并从中删除@Column 映射。以上是关于休眠映射异常:无法确定列的类型的主要内容,如果未能解决你的问题,请参考以下文章