0

I have one scenario as following

Class A {
    String id;
    List<B> lstB;
}

Class B {
    String id;
    String name;
}

Class C extends B {
    String location;
}

For above mapping here is my Hibernate mapping

<class name="A" table="A">
    <id name="id" type="java.lang.String" column="ID" length="50">
        <generator class="assigned" />
    </id>
    <set name="lstB" table="A_B" lazy="true">
        <key column="A_ID" />
        <many-to-many column="B_ID" class="B" />
    </set>
</class>

<class name="B" table="B">
    <id name="id" type="java.lang.String" column="ID" length="50">
        <generator class="assigned" />
    </id>
    <property name="name">
        <column name="NAME"/>
    </property>
    <discriminator column="DISCRIMINATOR" not-null="false" />
    <subclass name="C" discriminator-value="C">
        <property name="location">
            <column name="LOCATION"/>
        </property>             
    </subclass>
</class>

Now I want to retrieve List of B and C objects on the basis of following given parameters.

1) id given from A.

2) Location from C.

Now if result object is instance of B then just add in list but if it is instance of C then have to verify with Location name passed in where clause.

Can I achieve this using HQL? or I have to go with Criteria API? Can we check instance of object from HQL itself?

Navnath
  • 1,064
  • 4
  • 18
  • 34
  • You must use subclass? – Andrea Catania Feb 02 '16 at 12:36
  • List will contains supper class objects and subclass objects depending on where clause. – Navnath Feb 02 '16 at 12:50
  • Sorry, I've bad formulated my question. I've not too much experience with subclass but I think what you need cannot be done using only one query. So my question is for you it's necessary to use subclass? You can get the list B with one query but you need to change the association type. Otherwise wait more to see if one with more experienced about subclass comes to help you – Andrea Catania Feb 02 '16 at 13:33
  • I have to go with Subclass only. Using two queries is one option, But I am trying if this can be achieve with single. – Navnath Feb 02 '16 at 14:04
  • Looking back what you ask is totally impossible. Leaving hibernate in Java you cannot have a list of C class that contain the class B. While you can have a list of B that contain C because you can't distinguish Object of C from Object of B – Andrea Catania Feb 02 '16 at 15:01

0 Answers0