org.hibernate.MappingException: Could not determine type for: (foo)

644 views Asked by At

I have two classes being managed by Hibernate, ClassA and ClassB. ClassA has a reference to ClassB. According to the documentation Hibernate should be able to get the type through reflection. I can't understand why this doesn't work. Am I missing something really fundamental here?

This is ClassA marked with the @Entity annotation and holding a reference to 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;
    }   

}

This is ClassB marked with the @Enity annotation.

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;
    }

}

I get the following stack trace:

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

There are 1 answers

1
Magnilex On BEST ANSWER

You need to annotate the field with a @OneToOne (assuming it isn't a @ManyToOne, i.e. many ClassB can contain many ClassA's) annotation:

@OneToOne
private ClassB classB;

This should be the minimum code needed to properly add the relation between the entities.