-->
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.  [ 8 posts ] 
Author Message
 Post subject: getPropertyValue throws ClassCastException when using .
PostPosted: Mon Feb 27, 2006 10:49 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
I don't know if this is a bug or an undocumented "feature" of getPropertyValue. I had assumed that ClassMetadata.getPropertyValue would be smart enough to traverse nested properties using "." notation. It appears that it is, but only assumes that "." notation signifies the contents of a nested component instead of traversing other kinds of relationships, e.g. many-to-one (what I am trying to use it with). This is evidenced by the following code from AbstractEntityTuplizer:
Code:
      int loc = propertyPath.indexOf('.');
      String basePropertyName = loc>0 ?
         propertyPath.substring(0, loc) : propertyPath;
         
      int index = entityMetamodel.getPropertyIndex( basePropertyName );
      Object baseValue = getPropertyValue( entity, index );
      if ( loc>0 ) {
         ***ComponentType type = (ComponentType) entityMetamodel.getPropertyTypes()[index];***
         return getComponentValue( type, baseValue, propertyPath.substring(loc+1) );


The line with ***'s around it throws the expected ClassCastException when "." syntax is used to specify the property path of a ManyToOneType. Since the usage of this method with "." notation isn't specifically covered in the Javadoc, I wasn't sure whether this was intentional, or a valid bug/feature request. I can try to hack something together using BeanUtils in the meantime, but it would be easier if Hibernate was able to do this for me.

Relevant generic info:

Hibernate version:
3.1.2

Mapping documents:
Code:
... (snip) ...
         <many-to-one name="location" class="x.y.Location" column="LOCATION_ID"
               not-null="true" foreign-key="FK_JP_LOCATION"/>
... (snip) ...


mapping for x.y.Location object:
Code:
... (snip) ...
        <property name="name" column="NAME" type="string" length="100"/>
... (snip) ...


Full stack trace of any exception that occurs:
This occurs when I call getPropertyValue with the property name "location.name" on the object containing the location many-to-one relationship indicated above.

Code:
java.lang.ClassCastException at org.hibernate.tuple.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:282) at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:3252) at
...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 11:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
it does not support nested properties except for component/composite stuff

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 11:31 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
Would it be valid to submit a bug with a patch to the Javadoc so that it's clear that is the only reason "." should be used for property names passed to this method?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 11:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
sure...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 11:40 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
Isn't it kind of a mismatch, though, that the same "." syntax works fine within an HQL query? What is the intended purpose of the ClassMetadata.getPropertyValue API?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 11:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
to get the specifc value for the property on the class modelled after ClassMetaData.

and since those nested parts is not otherwise available, then it make sense to have them. (the nested stuff does not refer to ClassMetaData)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 11:57 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
Thanks max, I guess what I was thinking the intention of this API was (to do PropertyUtils-style navigation of "." notation properties) is better served by just using PropertyUtils against my Hibernate object directly. It might be useful (IMHO) to add some API that would let users do this without requiring the use of PropertyUtils - especially since there must be code to do nested property navigation for use with HQL.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 2:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
contribute a patch, but remember that it should only work for persistent properties....actually thinking about it we should probably just write a util that uses the existing api to do this and put it on wiki.

_________________
Max
Don't forget to rate


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