-->
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.  [ 6 posts ] 
Author Message
 Post subject: Hibernate getting query class confused
PostPosted: Thu Jun 16, 2005 10:09 pm 
Newbie

Joined: Tue Mar 08, 2005 7:11 pm
Posts: 9
Location: Sydney, Australia
Hi there,

I'm encountering an interesting problem where Hibernate is selecting from a different class to that supplied to the query. I've tried this out on all 3.0.x versions (up to 3.0.5), and with both HQL and Criteria queries.

I have a table called OFFICE. I also have a view called V_USER_OFFICES, which provides a restricted list of office IDs that a particular type of user can see (based on security rules that exists on the database).

The two corresponding objects are mapped as follows:

<hibernate-mapping package="au.com.whatever" schema="CORP">

<class name="Office" table="OFFICE" lazy="true" mutable="false">
<id name="officeID" type="int" column="OFC_NO"/>
<propertyname="name" type="java.lang.String" column="OFC_ENAME" length="40"/>
</class>

<class name="ViewOffices" table="V_USER_OFFICES" lazy="true" mutable="false">
<composite-id>
<key-many-to-one name="office" class="Office">
<column name="OFC_NO" />
</key-many-to-one>
</composite-id>
</class>

</hibernate-mapping>

I can query for ViewOffices objects fine, and I can also load an Office object fine using Session.get(), but if I try to load Office/s through queries (like the ones below), Hibernate substitues the ViewOffices class into the query instead!

EXAMPLE QUERIES:
from Office o order by o.name

from Office o where o.officeID = 123

Criteria ct = HIBERNATE.createCriteria(Office.class)
.add(Restrictions.eq("officeID", new Integer(officeID)));

EXAMPLE EXCEPTIONS:
org.hibernate.QueryException could not resolve property: name of: au.com.marsh.client.persistence.ViewOffices [ from au.com.marsh.client.persistence.ViewOffices o order by o.name]

org.hibernate.QueryException) could not resolve property: officeID of: au.com.marsh.client.persistence.ViewOffices [ from au.com.marsh.client.persistence.ViewOffices o where o.officeID = 21]


If I remove the ViewOffices mapping from my configuration, the Office queries work fine. Somehow Hibernate gets confused by the fact that the primary key of of ViewOffices is actually an Office...


Any ideas?

Troy.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 10:47 pm 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
Might be related to this????

http://forum.hibernate.org/viewtopic.php?t=943726


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 10:49 pm 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
Try this:

Change officeID to officeId

The case may be throwing off the property resolution algorithm. I've seen it happen before in other libraries.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 11:41 pm 
Newbie

Joined: Tue Mar 08, 2005 7:11 pm
Posts: 9
Location: Sydney, Australia
Thanks for your replies, but "officeID" is the correct case.

In any event I can quite easily work around the problem in my code, I just found it to be interesting behaviour and didn't know if anyone had seen a similar problem and pointed it out. It does indeed related to the post you referred too so...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 11:58 pm 
Newbie

Joined: Tue Mar 08, 2005 7:11 pm
Posts: 9
Location: Sydney, Australia
Sorry, my answer to last post wasn't really complete...

It does seem like like the problem may be related to post you linked too

*BUT*

the problem i'm seeing, which seems to be caused by a <key-many-to-one> mapping, is not affecting the object that contains that mapping, but the object that is referred to by that mapping.

i.e. the mapping of one object is rendering another object almost useless....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 17, 2005 12:49 am 
Newbie

Joined: Tue Mar 08, 2005 7:11 pm
Posts: 9
Location: Sydney, Australia
Well, by tracing through the Hibernate code I found the problem, and naturally it lead me back to something stupid that I completely overlooked...

I totally didn't realise that my ViewOffice object extends the Office object (doh!), but isn't mapped that way (nor should it be).

This creates a conflict in org.hibernate.impl.SessionFactoryImpl.getImplementors() where java.lang.Class.isAssignableFrom() gives a different answer to org.hibernate.persister.entity.Queryable.isInherited().

Thanks for your help though guys. I guess these are the breaks when you're trying resuscitate legacy code...


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