-->
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.  [ 6 posts ] 
Author Message
 Post subject: Mapping <set> of entities with where="" clau
PostPosted: Tue Oct 11, 2005 12:04 pm 
Beginner
Beginner

Joined: Sat Sep 17, 2005 6:50 am
Posts: 23
Hello, all users if Hibernate!

Unfortunately, I have met a problem I can't solve when mapping <set> collection with outer-join="true", which has where="" clause for it's entities.

1) When we use outer-join="false", hibernate fetches all records from many-to-many table hotels_images and then tries to fetch each instance of "entities.HotelImageImpl" one-by-one by it's id, but it fails due to where-constraint:

The generated SQL (<set outer-join="false">, where="type != 'PR'" in "entities.HotelImageImpl"):
Code:
15:05:43 INFO  [STDOUT] Hibernate: select hotelimage0_.id as id0_, hotelimage0_.md5 as md2_16_0_, hotelimage0_.type as type16_0_ from images hotelimage0_ where hotelimage0_.id=? and hotelimage0_.type != 'PR'
15:05:43 INFO  [DefaultLoadEventListener] Error performing load command
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [entities.HotelImageImpl#167986]


2) When we use outer-join="true", the where="" clause is not met in the query:

The generated SQL (<set outer-join="true">, where="type != 'PR'" in "entities.HotelImageImpl"):
Code:
16:09:07 INFO  [STDOUT] Hibernate: select images0_.hotelId as hotelId1_, images0_.imageId as imageId1_, hotelimage1_.id as id0_, hotelimage1_.md5 as md2_16_0_, hotelimage1_.type as type16_0_ from hotels_images images0_ inner join images hotelimage1_ on images0_.imageId=hotelimage1_.id where images0_.hotelId=?


3) When we use outer-join="true", using the where="" clause in <set>, it is associated with table hotels_images:

The generated SQL (<set outer-join="true" where="type != 'PR'">):
Code:
17:23:30 INFO  [STDOUT] Hibernate: select images0_.hotelId as hotelId1_, images0_.imageId as imageId1_, hotelimage1_.id as id0_, hotelimage1_.md5 as md2_16_0_, hotelimage1_.type as type16_0_ from hotels_images images0_ inner join images hotelimage1_ on images0_.imageId=hotelimage1_.id where  images0_.type != 'PR' and images0_.hotelId=?
17:23:30 WARN  [JDBCExceptionReporter] SQL Error: 1054, SQLState: 42S22
17:23:30 ERROR [JDBCExceptionReporter] Unknown column 'images0_.type' in 'where clause'



Hibernate version: 3.0.5

Mapping document for class "entities.HotelImpl"
Code:
<hibernate-mapping default-lazy="false">
   <class
      name="entities.HotelImpl"
      table="hotels"
      mutable="false"
   >
      <cache usage="read-only"/>

      <id name="id" type="int">
         <generator class="native"/>
      </id>

      <set name="images" table="hotels_images">
         <cache usage="read-only"/>
         <key column="hotelId" not-null="true"/>
         <many-to-many
            column="imageId" outer-join="true"
            class="entities.HotelImageImpl"
         />
      </set>
   </class>
</hibernate-mapping>


Mapping document for class "entities.HotelImageImpl"
Code:
<hibernate-mapping>
   <!-- The image type "PR" means "price table" and should be not loaded -->
   <class
      name="entities.HotelImageImpl"
      table="images"
      mutable="false"
      where="type != 'PR'"
   >
      <id name="id" type="int">
         <generator class="native"/>
      </id>

      <property name="name" column="md5" type="java.lang.String"/>
      <property name="type" type="java.lang.String"/>

   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
   session = getHibenateSessionFactory().openSession();
   session.setFlushMode(FlushMode.NEVER);
   session.get(entities.HotelImpl.class, Integer.valueOf(id));
   session.close();


Name and version of the database you are using: MySQL 4.1.10


Top
 Profile  
 
 Post subject: Mapping <set> of entities with where="" clau
PostPosted: Fri Oct 14, 2005 5:35 am 
Beginner
Beginner

Joined: Sat Sep 17, 2005 6:50 am
Posts: 23
Can anybody help with the subject?


Top
 Profile  
 
 Post subject: Mapping <set> of entities with where="" clau
PostPosted: Fri Nov 18, 2005 1:06 pm 
Beginner
Beginner

Joined: Sat Sep 17, 2005 6:50 am
Posts: 23
Please, help. Is it a bug or a feature?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 3:55 pm 
Regular
Regular

Joined: Thu Aug 19, 2004 9:28 am
Posts: 63
It's an old post but, I'm still gonna answer it just in case someone else has the same problem.

If you want an outer join and a WHERE clause on the same class to work, you have to write your WHERE clause in the mapping file where your association is.

Something like this:

<set name="applPartner" where="Role_Code=156">
<key column="Appl_ID"/>
<one-to-many class="hibernate.ApplParticipation"/>
</set>

This will result in something like this:

...outer join application on appl_id=appl_partner.appl_id and appl_partner.role_code = 156

cheers


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 9:27 am 
Beginner
Beginner

Joined: Sat Sep 17, 2005 6:50 am
Posts: 23
Thanks for the hint!

To make a followup, "where=" cluase in <set> influences the right-handed table for <one-to-many> associations and intermediate table for <many-to-many> associations. In order to define a clause to the right-most table in <many-to-many> association, you should use <many-to-many where=""> construction.

I've got one more question.
How can I define "where"-clause for <many-to-one> association?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 10:08 am 
Beginner
Beginner

Joined: Wed Oct 05, 2005 5:35 am
Posts: 47
Location: France
As far as I know - you can't.
Gavin from Hibernate team says:
"The where clause is not used at all during association navigation. I suspect you might be able to argue that it should be used for navigating many-to-many associations. If you think so, sibmit a feature request to JIRA and I'll give it some thought" (see http://forum.hibernate.org/viewtopic.php?t=189 ).


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