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
...