-->
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.  [ 2 posts ] 
Author Message
 Post subject: dynamic class with import Bug? Additional query being run
PostPosted: Thu Jan 13, 2005 1:48 am 
Newbie

Joined: Mon Dec 13, 2004 2:32 am
Posts: 8
Hi Everybody,

I have noticed a rather unusual and unnecessary execution of an additional query, when i am trying to query an entity using the dynamic-class feature which has a one-2-many relationship with another entity.

Here is the sample scenario i made up.
A CatOwner is an entity which is related to Cat entity with a one-2-many cardinality.
Both the table in my database contains two records. For my dummy example i just made sure that the catowner has atleast one cat associated with him.

When i use an HQL query "from CatOwnerPersistentMap catOwnerPersistentMap", as one could expect the query is supposed to return 2 catowner object, each catowner object containing a Set object which contains one Cat Object.

What exactly is happening is there are actually 4 object being returned in the list after the query. The list contains 2 catowner object (which contain a set object containing cat object), and 2 Cat object.

I noticed that there are actually 2 primary queries being synthesized and executed for the above mentioned HQL, (See the queries executed below in the post).

With some debugging i kind of narrowed down to the method 'public static String[] concreteQueries(String query, SessionFactoryImplementor factory) throws MappingException' in org.hibernate.hql.QuerySplitter.java Which i suspect might be the cause of the bug,

Here is piece of code within the method which is of particular interest,
/******************************************/
String importedClassName= getImportedClass(token, factory);
if (importedClassName!=null) {
String[] implementors = factory.getImplementors(importedClassName);
String placeholder = "$clazz" + count++ + "$";
if ( implementors!=null ) {
placeholders.add(placeholder);
replacements.add(implementors);
}
token = placeholder;
}

Here there are 2 'implementors' being returned from the method call factory.getImplementors(importedClassName); which finally results in generation of 2 primary queries.
/*****************************************/

I have not thought about a fix for the above problem yet.

I ran the same sample scenario with the regular POJOs they work perfectly.

It would be really great, if any of you could look into the above issue and confirm the problem or point out where exactly have i gone wrong.

!!! NOTE !!!! :
Due to an existing bug in the Hibernate 3.0 alpha it might not be possible to get the one-2-many and many-2-many relationship code running with dynamic class.
Bug entry URL :http://opensource.atlassian.com/projects/hibernate/browse/HHH-81

I made a really !!! crude fix to atleast get the code running. The fix has been mentioned as comment to the above mentioned bug.

Hibernate version: 3.0 alpha

Mapping documents:
CatOwner.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 auto-import="false">
<import class="java.util.Map" rename="CatOwnerPersistentMap"/>
<dynamic-class entity-name="CatOwnerPersistentMap" table="CatOwner" discriminator-value="CO" persister="org.hibernate.persister.SingleTableEntityPersister">

<id name="owner_id" type="string" unsaved-value="null" >
<column name="OWNER_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>

<property name="name" type="string"/>

<set name="cats" inverse="false" lazy="false" cascade="all">
<key column="owner_id"/>
<one-to-many class="CatPersistentMap"/>
</set>

</dynamic-class>

</hibernate-mapping>


Cat.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 auto-import="false" >
<import class="java.util.Map" rename="CatPersistentMap"/>
<dynamic-class entity-name="CatPersistentMap" table="Cat" discriminator-value="C" persister="org.hibernate.persister.SingleTableEntityPersister">

<id name="id" type="string" unsaved-value="null" >
<column name="CAT_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>

<property name="name" type="java.lang.String">
<column name="NAME" length="16" not-null="true"/>
</property>

<property name="sex" type="char"/>

<property name="weight" type="float" />

<many-to-one name="CatOwner" column="owner_id" class="CatOwnerPersistentMap" />

</dynamic-class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
transaction = session.beginTransaction();
String hqlQuery = "from CatOwnerPersistentMap catOwnerPersistentMap";
Query query = session.createQuery(hqlQuery);
List catOwnerList = query.list();
System.out.println("Query Data :: " + catOwnerList.size());

Full stack trace of any exception that occurs:

Name and version of the database you are using: Oracle 9i

The generated SQL (show_sql=true):
Hibernate: select catownerpe0_.OWNER_ID as OWNER_ID, catownerpe0_.name as name1_ from CatOwner catownerpe0_
Hibernate: select cats0_.owner_id as owner_id__, cats0_.CAT_ID as CAT_ID__, cats0_.CAT_ID as CAT_ID0_, cats0_.NAME as NAME0_0_, cats0_.sex as sex0_0_, cats0_.weight as weight0_0_, cats0_.owner_id as owner_id0_0_ from Cat cats0_ where cats0_.owner_id=?
Hibernate: select cats0_.owner_id as owner_id__, cats0_.CAT_ID as CAT_ID__, cats0_.CAT_ID as CAT_ID0_, cats0_.NAME as NAME0_0_, cats0_.sex as sex0_0_, cats0_.weight as weight0_0_, cats0_.owner_id as owner_id0_0_ from Cat cats0_ where cats0_.owner_id=?
Hibernate: select cats0_.owner_id as owner_id__, cats0_.CAT_ID as CAT_ID__, cats0_.CAT_ID as CAT_ID0_, cats0_.NAME as NAME0_0_, cats0_.sex as sex0_0_, cats0_.weight as weight0_0_, cats0_.owner_id as owner_id0_0_ from Cat cats0_ where cats0_.owner_id=?
Hibernate: select cats0_.owner_id as owner_id__, cats0_.CAT_ID as CAT_ID__, cats0_.CAT_ID as CAT_ID0_, cats0_.NAME as NAME0_0_, cats0_.sex as sex0_0_, cats0_.weight as weight0_0_, cats0_.owner_id as owner_id0_0_ from Cat cats0_ where cats0_.owner_id=?
Hibernate: select cats0_.owner_id as owner_id__, cats0_.CAT_ID as CAT_ID__, cats0_.CAT_ID as CAT_ID0_, cats0_.NAME as NAME0_0_, cats0_.sex as sex0_0_, cats0_.weight as weight0_0_, cats0_.owner_id as owner_id0_0_ from Cat cats0_ where cats0_.owner_id=?
Hibernate: select cats0_.owner_id as owner_id__, cats0_.CAT_ID as CAT_ID__, cats0_.CAT_ID as CAT_ID0_, cats0_.NAME as NAME0_0_, cats0_.sex as sex0_0_, cats0_.weight as weight0_0_, cats0_.owner_id as owner_id0_0_ from Cat cats0_ where cats0_.owner_id=?
Hibernate: select catpersist0_.CAT_ID as CAT_ID, catpersist0_.NAME as NAME0_, catpersist0_.sex as sex0_, catpersist0_.weight as weight0_, catpersist0_.owner_id as owner_id0_ from Cat catpersist0_

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 12:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Fixed in CVS. This would have been better as a JIRA issue.


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