-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Querying many-to-many relations
PostPosted: Tue Jan 09, 2007 4:57 pm 
Newbie

Joined: Fri Sep 30, 2005 12:45 am
Posts: 8
Hi All,
I have mapped a many-to-many relation across a join table as follows,

The database structure is as follows
Case (1-M ) - CASE_CASE_PROFILE (M-1) - CODED_CASE_PROFILE

In the Case.hbm file, the set is mapped as follows:

Hibernate version:3.0
Mapping documents:
Code:
<class name="com.unyric.cias.cm.data.domain.Case" table="CASE">
     
    <id name="id" type="java.lang.Long" column="CASE_ID">       
       <generator class="sequence">
         <param name="sequence">CASE_SEQ</param>
      </generator>
    </id>
   
<property name="name" column="CASE_DESC" type="java.lang.String"/>

<set name="caseProfiles" table="CASE_CASE_PROFILE"
       cascade="all-delete-orphan" lazy="true">
         <key column="CASE_ID"/>
         <many-to-many column="CASE_PROFILE_CODE" class="com.unyric.cias.cm.data.domain.type.CaseProfileType"/>
    </set> 
</class>



I am trying to query the caseProfiles i.e get all the Cases for a Coded_Profile type by using restrictions
I tried creating an ArrayList with the CaseProfileType however the join is on the CASE_ID column. I do not have the
middle object with the CASE_ID. Is it even possible to use restrictions or should I use some other way to implement this.
The main reason I need to use restrictions is this is one of the fields in a search screen with multiple
parameters.

Code:
List<CaseProfileType> caseProfilesList = new ArrayList<CaseProfileType>();
caseProfilesList.add(caseCriteria.getCaseProfileType());
      

  Criteria criteria  = session.createCriteria(CiasCase.class);
  criteria.setFetchMode("caseProfiles", FetchMode.JOIN);
  criteria.add(Restrictions.in( "caseProfiles",caseProfilesList ) );



Code:
    select
        this_.CASE_ID as CASE1_0_7_,
        this_.CASE_DESC as CASE2_0_7_,
        this_.CASE_NUM as CASE3_0_7_,
        caseprofil5_.CASE_ID as CASE1_10_,
        caseprofil6_.CASE_PROFILE_CODE as CASE2_10_,
        caseprofil6_.CASE_PROFILE_CODE as CASE1_16_3_,
        agency9_.ADDRESS_ID as ADDRESS6_58_6_
    from
        CASE this_
        left outer join
            CASE_CASE_PROFILE caseprofil5_
                on this_.CASE_ID=caseprofil5_.CASE_ID
        left outer join
            CODED_CASE_PROFILE caseprofil6_
                on caseprofil5_.CASE_PROFILE_CODE=caseprofil6_.CASE_PROFILE_CODE
        where
            this_.CASE_ID in (
                ?
            )




Thanks in advance for any help, and please pardon if its not clear.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 10, 2007 10:28 am 
Newbie

Joined: Fri Sep 30, 2005 12:45 am
Posts: 8
Hi All,

I found the solution to this, basically I had to add the alias

Code:
Criteria criteria  = session.createCriteria(CiasCase.class);
  criteria.createAlias("caseProfiles","profiles");       
  criteria.add(Restrictions.eq( "profiles.key",caseCriteria.getCaseProfileType().getKey()  ) );


This generates the right sql like a charm!

Code:
select
        this_.CASE_ID as CASE1_0_7_,
        caseprofil6_.CASE_ID as CASE1_10_,
        profiles1_.CASE_PROFILE_CODE as CASE2_10_,
        profiles1_.CASE_PROFILE_CODE as CASE1_16_3_,
        profiles1_.DESCRIPTION as DESCRIPT2_16_3_,
        profiles1_.ACTIVE_IND as ACTIVE3_16_3_,

        agency10_.ADDRESS_ID as ADDRESS6_58_6_
    from
        CASE this_
        inner join
            CASE_CASE_PROFILE caseprofil6_
                on this_.CASE_ID=caseprofil6_.CASE_ID
        inner join
            CODED_CASE_PROFILE profiles1_
                on caseprofil6_.CASE_PROFILE_CODE=profiles1_.CASE_PROFILE_CODE
        left outer join
        where
            profiles1_.CASE_PROFILE_CODE=?


Ref:
http://www.jroller.com/page/wakaleo/?an ... iteria_api

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.