Вопрос

I am using a POJO to define two tables (Main and backup). Both XML Classes are refering to same Java Class.

Reference : Mapping same POJO to more than one table in Hibernate XML mapping files

<hibernate-mapping>
    <class name="com.mypackage.model.Table1" table="Table1" entity-name="Table1">
        <id name="Id" type="java.lang.Long">
            <column name="ID" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="20" />
        </property>
        <property name="age" type="int">
            <column name="AGE" />
        </property>
    </class>

    <class name="com.mypackage.model.Table1" table="Table2" entity-name="Table2">
        <id name="Id" type="java.lang.Long">
            <column name="ID" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="20" />
        </property>
        <property name="age" type="int">
            <column name="AGE" />
        </property>
    </class>
</hibernate-mapping>

POJO Defination:

public class Table1 implements Serializable {

    private static final long serialVersionUID = -594654774484987124L;

    private Long id;
    private String Name;
    private int Age;
// Getters and setters.... Removed...
}

I am able to create to different tables. And also using

   session.saveOrUpdate(entityName, myObject)

API I am able to add new records these table individually.

While retrieving, I am using HQL

Query query = session.createQuery("from Table1");
list = query.list();

It returns me all records in Table1 as well as Table2. If I tried to give Table2, then Error is showed "Table2 is not mapped"

Is there any to retrive records from ONLY table1 and Table2 individually.

Это было полезно?

Решение

add attribute polymorphism="explicit" to u r class tag.

code :

 <class name="com.mypackage.model.Table1" table="Table1" entity-name="Table1" polymorphism="explicit">
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top