Mapping two or more hibernate hbm files to a single java pojo class

559 views Asked by At

hbm.xml file for table A whose pojo class in MyClass

<class name="com.entities.MyClass" table="A" entity-name="tableOne">
    <id name="id" access="field" type="java.lang.Long">
        <column name="ID" />
        <generator class="increment" />
    </id>
    <property name="columnOne" type="java.lang.String">
        <column name="COLUMN_ONE" />
    </property>
    <property name="columnTwo" type="java.lang.String">
        <column name="COLUMN_TWO" />
    </property>
</class>

hbm.xml file for table B (it also has the same pojo class "MyClass")

<class name="com.entities.MyClass" table="B" entity-name="tableTwo">
    <id name="id" access="field" type="java.lang.Long">
        <column name="ID" />
        <generator class="increment" />
    </id>
    <property name="columnOne" type="java.lang.String">
        <column name="COLUMN_ONE" />
    </property>
    <property name="columnTwo" type="java.lang.String">
        <column name="COLUMN_TWO" />
    </property>
</class>

this is the POJO class

package com.entities;

public class MyClass{

    Long id;
    String columnOne;
    String columnTwo;
    public MyClass() {
        // TODO Auto-generated constructor stub
    }
    public MyClass(Long id, String columnOne, String columnTwo) {
        super();
        this.id = id;
        this.columnOne = columnOne;
        this.columnTwo = columnTwo;
    }
    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }
    /**
     * @return the columnOne
     */
    public String getColumnOne() {
        return columnOne;
    }
    /**
     * @param columnOne the columnOne to set
     */
    public void setColumnOne(String columnOne) {
        this.columnOne = columnOne;
    }
    /**
     * @return the columnTwo
     */
    public String getColumnTwo() {
        return columnTwo;
    }
    /**
     * @param columnTwo the columnTwo to set
     */
    public void setColumnTwo(String columnTwo) {
        this.columnTwo = columnTwo;
    }
}

How can I insert into the tables? I tried the following code :

session.save("tableOne",myClassObj);
transaction.commit();

but on commit it throws the following exception

Hibernate: select max(ID) from A

org.hibernate.MappingException: Unknown entity: com.entities.MyClass
1

There are 1 answers

0
Lakhan Singh On

you can use by table name as follows

session.save("A",myClassObj);
transaction.commit();