-->
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.  [ 3 posts ] 
Author Message
 Post subject: Searching Entity with one of the composite id
PostPosted: Wed Aug 16, 2006 11:17 pm 
Newbie

Joined: Tue Jul 11, 2006 10:41 pm
Posts: 11
Hi,

Supposed I Have 3 table , CUSTOMER , EVENT , CUSTOMER_EVENT
where CUSTOMER can participate in MANY EVENT
and ONE EVENT can HOST MANY CUSTOMER

My Hibernate Mapping for CUSTOMER_EVENT as below
Code:
<hibernate-mapping>
    <class name="com.biz.CustomerEvent" table="CUSTOMER_EVENT">
        <composite-id name="id" class="com.biz.CustomerEventId">
            <key-property name="customerId" type="string">
                <column name="CUSTOMER_ID" length="10" />
            </key-property>
            <key-property name="eventId" type="big_decimal">
                <column name="EVENT_ID" precision="22" scale="0" />
            </key-property>
        </composite-id>
        <many-to-one name="customer" class="com.biz.Customer" update="false" insert="false" fetch="select">
            <column name="CUSTOMER_ID" length="10" not-null="true" />
        </many-to-one>
        <many-to-one name="event" class="com.biz.Event" update="false" insert="false" fetch="select">
            <column name="EVENT_ID" precision="22" scale="0" not-null="true" />
        </many-to-one>
    </class>
</hibernate-mapping>



The question is , lets say i want to list all customer name that participate in event id = 1

What i did is
Code:
select ce from CustomerEvent ce inner join ce.event as ide where
             ide.eventId = 1




Above works well.. But when i do

Code:
select ce from CustomerEvent ce inner join ce.id as ide
  where ide.eventId = 1
it gives me
org.hibernate.hql.ast.QuerySyntaxError: Invalid path: 'ide.eventId'


Is there any way to select from one of the composite id.
How to construct the HQL and the corressponding Criteria.


i do something like below
Code:
session.createCriteria(CustomerEvent.class)
                .setAlias("id","cid")
               .add(Restrictions.eq("cid.eventId",new Long(1))).list();



also give me exceptions...



Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 3:27 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
I think both of these will work:
Code:
select
   ce
from
   CustomerEvent ce
where
   ce.id.eventId = 1

Code:
from
   CustomerEvent
where
   id.eventId = 1


Please, don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 3:30 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
And for Criteria:
Code:
session.createCriteria(CustomerEvent.class)
   .add(
      Restrictions.eq("id.eventId", new Long(1))
   )
   .list();

Did it work?

Please, don't forget to rate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.