Hibernate Query with entity-name

六眼飞鱼酱① 提交于 2020-01-05 05:48:06

问题


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.


回答1:


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

code :

 <class name="com.mypackage.model.Table1" table="Table1" entity-name="Table1" polymorphism="explicit">


来源:https://stackoverflow.com/questions/13767125/hibernate-query-with-entity-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!