Query cache fails on many-to-many select

Description

I have two entities, User and Group, with many-to-many relation:

-------------------------------------------------------------------------------------------------- User.hbm.xml
<?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="test.hibernate">
<class name="User" table="`USER`">
<id name="id" type="long" unsaved-value="null">
<column name="USER_ID" not-null="true"/>
<generator class="native"/>
</id>

<property name="login">
<column name="LOGIN" not-null="true" length="255"/>
</property>

<bag name="groups" table="USER_GROUP" lazy="false" cascade="none">
<key column="USER_ID"/>
<many-to-many class="Group" column="GROUP_ID"/>
</bag>
</class>
</hibernate-mapping>

-------------------------------------------------------------------------------------------------- Group.hbm.xml
<?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="test.hibernate">
<class name="Group" table="`GROUP`">
<id name="id" type="long" unsaved-value="null">
<column name="GROUP_ID" not-null="true"/>
<generator class="native"/>
</id>

<property name="name">
<column name="GROUPNAME" not-null="true" length="100"/>
</property>

<bag name="users" table="USER_GROUP" lazy="true" inverse="true" cascade="all-delete-orphan">
<key column="GROUP_ID"/>
<many-to-many class="User" column="USER_ID"/>
</bag>
</class>

<query name="Group.select.by.login" cacheable="true"><![CDATA[
select user.groups from User user where user.login=?
]]>
</query>
</hibernate-mapping>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- test.hibernate.User and test.hibernate.Group classes are just simple beans.

In Group.hbm.xml there is a query called "Group.select.by.login", it's intended to be cached. So I add corresponding properties to Hibernate configuraition:
-------------------------------------------------------------------------------------------------- properties.put("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
properties.put("hibernate.cache.use_query_cache", "true");
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Configuration has no extra parameters.

Then I'm trying to execute the query two times:
-------------------------------------------------------------------------------------------------- test fragment
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = null;

try {
session = sessionFactory.openSession();
Query query = session.getNamedQuery("Group.select.by.login");
List<Group> groups;

groups = query.setParameter(0, "admin").list(); // +++++++++++++++This list() returns list of two Group objects, ok +++++++++++++++
System.out.println("-----------------------------------------------------------------------------------");
for (Group group : groups) {
System.out.println(group.getName());
}
System.out.println("-----------------------------------------------------------------------------------");

groups = query.setParameter(0, "admin").list(); // +++++++++++++++This list() returns list of two nulls +++++++++++++++
System.out.println("-----------------------------------------------------------------------------------");
for (Group group : groups) {
System.out.println(group.getName());
}
System.out.println("-----------------------------------------------------------------------------------");
} finally {
if (session != null) {
session.close();
}
sessionFactory.close();
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- The first query returns list of, in my case, two groups. The second query returns list of two nulls instead of the same list.

I've investigated the problem, and found the following:

Query return value is "user.groups", and during query analysis (org.hibernate.hql.ast.QueryTranslatorImpl:160) query return type is defined as "org.hibernate.type.BagType(test.hibernate.User.groups)". This seems to be the problem, as during query put operation org.hibernate.CollectionType.disassemble is called (org.hibernate.cache.StandardQueryCache:80), which simply returns null as owner is null.

If this query is not a misuse, then I think, something is to be done at the time of analysis, so that return type be somewhat different.

Thanks.

Attachments

1

Activity

Brett MeyerJuly 8, 2014 at 3:11 PM

Bulk rejecting stale issues. If this is still a legitimate issue on ORM 4, feel free to comment and attach a test case. I'll address responses case-by-case. Thanks!

Brett MeyerApril 7, 2014 at 5:42 PM

In an effort to clean up, in bulk, tickets that are most likely out of date, we're transitioning all ORM 3 tickets to an "Awaiting Test Case" state. Please see http://in.relation.to/Bloggers/HibernateORMJIRAPoliciesAndCleanUpTactics for more information.

If this is still a legitimate bug in ORM 4, please provide either a test case that reproduces it or enough detail (entities, mappings, snippets, etc.) to show that it still fails on 4. If nothing is received within 3 months or so, we'll be automatically closing them.

Thank you!

Eric BrunNovember 7, 2012 at 11:10 PM

Hi,

I have the same issue with Hibernate 3.5.6 in 2012 !

Eric

Former userMay 15, 2010 at 12:29 AM

Is this still an issue using 3.5.2?

Dwayne DMay 13, 2010 at 10:51 AM

Does anyone know if this will be fixed in the near future?
Thanks in advance.

Rejected

Details

Assignee

Reporter

Components

Affects versions

Priority

Created October 16, 2007 at 5:24 PM
Updated July 8, 2014 at 3:11 PM
Resolved July 8, 2014 at 3:11 PM