I have a Role entity which has a many-to-one relationship into a Group entity. Since roles and groups are seldom changed, I wanted to implement a cache strategy for when retrieving a list of all roles with its groups.
Both entities are cacheable, so I thought to use the criteria query below, thinking that the main select will retrieve all the roles, and then when trying to retrieve every group, they would be picked-up from 2nd level cache (or SELECTed if it's not there yet).
However, no SELECT is executed after the main one, nor the groups got from the cache, Hibernate simply places a proxy for every group, and my app crashes when tries to show the list on screen. Please see the log excerpt to confirm this.
If I use FetchMode.JOIN instead of FetchMode.SELECT, an INNER JOIN is generated and everything works, but I understand this is not so efficient in terms of cache usage.
Am I doing something wrong, forgetting anything, or is this a bug?
Thanks,
Alex.
Hibernate version:
3.1.2
Annotations 3.1.0 beta 8
Mapping documents:
Code:
@Entity
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class MutableRole implements Role {
...
@ManyToOne(targetEntity=MutableGroup.class, fetch=FetchType.LAZY)
@JoinColumn(name="GROUP_TYPE_ID", nullable=false)
public Group getGroup() {
return group;
}
}
@Entity
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class MutableGroup implements Group {
...
}
Code between sessionFactory.openSession() and session.close():Code:
roles = session.createCriteria(MutableRole.class).setFetchMode("group", FetchMode.SELECT).list();
Full stack trace of any exception that occurs:Name and version of the database you are using:Oracle 10g
The generated SQL (show_sql=true):Code:
Hibernate: /* criteria query */ select this_.id as id8_0_, this_.name as name8_0_, this_.description as descript4_8_0_, this_.GROUP_TYPE_ID as GROUP5_8_0_, this_.ROLE_TYPE as ROLE1_8_0_ from ROLES this_
Debug level Hibernate log excerpt:Code:
182771 [http-8080-2] DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [tv.nation217.phoenix.model.impl.MutablePhoenixRole#81]
182771 [http-8080-2] DEBUG org.hibernate.engine.CollectionLoadContext - creating collection wrapper:[tv.nation217.phoenix.model.impl.MutableRole.permissions#81]
182771 [http-8080-2] DEBUG org.hibernate.event.def.DefaultLoadEventListener - loading entity: [tv.nation217.phoenix.model.impl.MutableGroup#81]
182771 [http-8080-2] DEBUG org.hibernate.event.def.DefaultLoadEventListener - creating new proxy for entity
182771 [http-8080-2] DEBUG org.hibernate.engine.TwoPhaseLoad - adding entity to second-level cache: [tv.nation217.phoenix.model.impl.MutablePhoenixRole#81]
182771 [http-8080-2] DEBUG org.hibernate.cache.NonstrictReadWriteCache - Caching: tv.nation217.phoenix.model.impl.MutableRole#81
182771 [http-8080-2] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [tv.nation217.phoenix.model.impl.MutablePhoenixRole#81]