Hibernate映射异常:无法确定列

时间:2015-07-05 18:17:40

标签: java hibernate hibernate-mapping

我想在我的两个类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类是:( fyi:一开始我不需要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:UML

2 个答案:

答案 0 :(得分:1)

假设数据库中有CustomerGroup个reprsents CustomerGroup表,请添加以下提到的注释:

@Entity
@Table(name="CustomerGroup")
public class CustomerGroup

答案 1 :(得分:0)

你必须在CustomerCustomerGroup之间告诉Hibernate你想要哪种关联:一对一或多对一。

此外,默认情况下,所有属性都按照JPA标准进行映射(这就是为什么如果使用JPA注释,Hibernate必须遵守它)。

如果你想让属性瞬态化,你必须明确地做到这一点:

@Transient
public BonusPackage getBonusPackage() {
    return bonusPackage;
}