org.hibernate.MappingException:无法确定:(foo)的类型

时间:2015-06-25 11:54:11

标签: java hibernate one-to-one

我有两个类由Hibernate管理,ClassAClassBClassA引用了ClassB。根据文档,Hibernate应该能够通过反射获得类型。我无法理解为什么这不起作用。我错过了一些非常重要的东西吗?

这是ClassA标有@Entity注释并持有对ClassB的引用。

package dom;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class ClassA {

    private ClassB classB;

    private long id;        

    public ClassA() {};

    public ClassB getClassB() {
        return classB;
    }

    public void setClassB(ClassB classB) {
        this.classB = classB;
    }

    @Id
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }   

}

ClassB标有@Enity注释。

package dom;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class ClassB {

    private long id;

    @Id
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

}

我得到以下堆栈跟踪:

Caused by: org.hibernate.MappingException: Could not determine type for: dom.ClassB, at table: ClassA, for columns: [org.hibernate.mapping.Column(classB)]
        at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:316)
        at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:294)
        at org.hibernate.mapping.Property.isValid(Property.java:238)
        at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:469)
        at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
        at org.hibernate.cfg.Configuration.validate(Configuration.java:1294)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1742)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1788)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)

1 个答案:

答案 0 :(得分:2)

您需要使用@OneToOne注释该字段(假设它不是@ManyToOne,即许多ClassB可以包含许多ClassA&#39}注释:

@OneToOne
private ClassB classB;

这应该是正确添加实体之间关系所需的最小代码。