0

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
Afnan alam
  • 9
  • 1
  • 1
  • 12
  • 1
    Can't you just create 2 extension for the MyEntity class? MyEntityA and MyEntityB – StanislavL May 22 '17 at 05:51
  • 1
    [this](http://stackoverflow.com/questions/22668350/how-to-map-one-class-with-multiple-tables-in-hibernate-javax-persistance) uses annotations but maybe you can adapt it – XtremeBaumer May 22 '17 at 06:16

1 Answers1

0

you can use by table name as follows

session.save("A",myClassObj);
transaction.commit();
Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33