我想在我的两个类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>
我在stackoverflow上检查了几个常见问题解答和其他线程,主要是注释存在问题。我试图在代码中修复它,但是我无法完成..:/
为了更好的说明,我在这里有一个UML:
最佳答案
假设类CustomerGroup
表示数据库中的CustomerGroup
表,请添加以下提到的注释:
@Entity
@Table(name="CustomerGroup")
public class CustomerGroup