OutOfMind
OutOfMind

Reputation: 924

Many To One relation inside Embeddable , Hibernate

I have a scenario where an Embeddable class in hibernate uses anEntity. According to various answers I found on SO and other links, we can write @ManyToOne, @OneToMany inside an Embeddable class.

But doing this gives me HibernateMappingExeption

Consider the following example: I have two Entities and an Embeddable class as under:

Entity A

@Entity
@Table(name = "A")
public class A {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int a_id;

    @ElementCollection
    @JoinTable(name = "embeded_class_table", joinColumns = @JoinColumn(name = "a_id"))
    private List<EmbeddedClass> embeddedClass;

Entity B

@Entity
@Table(name = "B")
public class B {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int b_id;

Embeddable Class which uses Entity B

@Embeddable
public class EmbeddableClass {

    @ManyToOne
    @JoinColumn(name = "b_id")
    private B b;

The error I am getting is as under:

org.hibernate.MappingException: Could not determine type for: app.model.B, at table: embeded_class_table, for columns: [org.hibernate.mapping.Column(b)]

Could anyone please suggest if I am using these stuff correctly and if yes, what am I missing?

Upvotes: 4

Views: 8154

Answers (1)

Elias
Elias

Reputation: 694

Assuming your scenario I tried the following I didn't get any issues:

@Entity
@Table(name = "A")
public class A {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int a_id;

    @ElementCollection
    @JoinTable(name = "embeded_class_table", joinColumns = @JoinColumn(name = "a_id"))
    private List<EmbeddableClass> embeddedClass;

    public int getA_id() {
        return a_id;
    }

    public void setA_id(int a_id) {
        this.a_id = a_id;
    }

    public List<EmbeddableClass> getEmbeddedClass() {
        return embeddedClass;
    }

    public void setEmbeddedClass(List<EmbeddableClass> embeddedClass) {
        this.embeddedClass = embeddedClass;
    }

}

@Entity
@Table(name = "B")
public class B {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int b_id;
}

@Embeddable
public class EmbeddableClass {

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "b_id")
    private B b;

    public B getB() {
        return b;
    }

    public void setB(B b) {
        this.b = b;
    }


}

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.password">xxxx</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="hibernate.connection.username">elias</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.hbm2ddl.auto">create-drop</property>
        <property name="show_sql">true</property>
        <mapping class="com.springex.dto.A"></mapping>
        <mapping class="com.springex.dto.B"></mapping>
        <mapping class="com.springex.dto.EmbeddableClass"></mapping>
    </session-factory>
</hibernate-configuration>

Upvotes: 2

Related Questions