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