Monday 9 January 2012

How to retrive data's in Many-to-Many Relationship using Hql?

You Can Retrive all data in Many-To-Many Relationship Using the following join Query

Hbm File is Like these


<?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>

<joined-subclass entity-name="test"
                 table="TEST"  dynamic-update="true" dynamic-insert="true">

<key column="ID"/>

<property name="name" type="string" column="NAME" />

<set name="assistantName"
        table="TEST_1"
       inverse="false"
       lazy = "false"
       cascade="none"
       sort="unsorted">
       <key column="ID"/>
       <element column="ASSISTANTNAME"  type="string"/>
        </set>
</joined-subclass>
</hibernate-mapping>



Use Of Following Hql  join Query  Retrive Both Table data TEST and TEST_1


     
select
        fetchInfo.id as id,
        fetchInfo.name as name,
        Test_1.id as assistantName
from
         test as fetchInfo join fetchInfo.test_1 as Test_1
where
         fetchInfo.id =:someValue