0

I do have a hibernate mapping which looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.lwl.anlei.bl.model.imp">  

   <class name="PersonFo">
     <id name="id" type="integer"/>     
     <property name="name" type="string"/>
     <property name="info" type="string"/>
   </class> 

  <sql-query name="person1">
    <return alias="gb" class="PersonFo"/>
    SELECT id          as {gb.id},
           name        as {gb.name},
           info        as {gb.info}
    FROM   table
    WHERE  field1 = :param
  </sql-query>

  <sql-query name="person2">
    <return alias="gb" class="PersonFo"/>
    SELECT id          as {gb.id},
           second_name as {gb.name},
           whatever    as {gb.info}
    FROM   table
    WHERE  field2 = :param
  </sql-query>

</hibernate-mapping>

This is the simplyfied hbm of what I do have. In Java I am getting my data in this way, with the "getQueryName":

List<Person> myPersons =
    session.getNamedQuery("person1").setString("param", "important text").list();

Now I have to use views, instead of these querys. I know, I could simply write "SELECT xxx FROM view_person1" and "SELECT xxx FROM view_person2" into the sql-querys, but I would prefer to use something like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.lwl.anlei.bl.model.imp">  

   <class name="PersonFo" 
    table="view_person1"
    entity-name="view_person1" >
     <id name="id" column="id" type="integer"/>     
     <property name="name" column="name" type="string"/>
     <property name="info" column="info" type="string"/>
   </class> 

   <class name="PersonFo" 
    table="view_person2"
    entity-name="view_person2" >
     <id name="id" column="id" type="integer"/>     
     <property name="name" column="name" type="string"/>
     <property name="info" column="info" type="string"/>
   </class> 

</hibernate-mapping>

In another example, I saw how to save data via hibernate this way. Map Two Identical tables ( same schema...) to same entity in Hibernate

They simply used:

_session.Save("view_person1", xxxx) 
_session.Save("view_person2", xxxx)

But, how do I QUERY data? Is there something like:

List<Person> myPersons =
    session.getNamedEntity("view_person2").list();

Help will be very appriciated! Thanks!

1 Answer 1

1

Use polymorphism="explicit" to differentiate your named entities:

<class name="PersonFo" 
    table="view_person1"
    entity-name="view_person1" polymorphism="explicit" >
     <id name="id" column="id" type="integer"/>     
     <property name="name" column="name" type="string"/>
     <property name="info" column="info" type="string"/>
   </class> 

   <class name="PersonFo" 
    table="view_person2"
    entity-name="view_person2" >
     <id name="id" column="id" type="integer"/>     
     <property name="name" column="name" type="string"/>
     <property name="info" column="info" type="string"/>
   </class> 

and query your named entities like below:

List list1 = session.createQuery("from view_person1").list();
List list2 = session.createQuery("from view_person2").list();
Sign up to request clarification or add additional context in comments.

1 Comment

I'm gonna test that. Thanks in advance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.