Dear all,
Hibernate version:3.2.5
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.intralot.bon.entities.config">
<class name="GameClass" table="GAME_CLASS" discriminator-value="0">
<cache usage="read-write" />
<id name="id" column="GAME_CLASS_ID">
<generator class="sequence">
<param name="sequence">GAME_CLASS_SEQ</param>
</generator>
</id>
<discriminator column="GAME_CLASS_TYPE" />
<version name="version" column="VERSION"/>
....
<many-to-one cascade="all" name="name" column="GAME_CLASS_NAME" not-null="true" />
<many-to-one cascade="all" name="description" column="GAME_CLASS_DSC" not-null="true" />
....
</hibernate-mapping>
Named Query:
Code:
In an hbm.xml file (Game.hbm.xml) I have the query:
<query name="getGames">
<![CDATA[
select
xgc
from
GameClass xgc inner join fetch xgc.name inner join fetch xgc.description
where
xgc.id = :classId
]]>
</query>
As you can see in the mapping document, the name and description attributes have many-to-one relationships with other classes.
What I need is to have the name and description in the results that the query will return. The simplest solution to that is to add an attribute lazy="false" in the above two elements in the GameClass.hbm.xml file and to remove the inner join fetches from the query. It works fine.
But, this solution does not harmonize with my project and I need another one. What am I need is to manipulate this task programmatically without the previously referred solution.
Not that I use the ehCache as a second level cache and also when I try to execute the query I use query.setCacheable(true) in order to have the query cacheable.
The problem is that: The first time that the query is executed the results are correct. I get back description and name. But when I try to execute the query again, unfortunately I have no results back. So, the caching of results does not work.
Note that I make a call out of an EJB container to a Session Bean which includes a method that executes the query. This method opens hibernate session, caches the results and then after closing the session, it returns the results to the caller. When I try to recall the same method with the same query (second time), then I get back short results, without the name and description. Maybe, hibernate should realize a join fetch in the cache in order to return the whole results.
Could you please anyone suggest me a solution to that problem?
Many thanks