[b]Hibernate version: 3.0[/b]
I have a situation where I have certain fields that appear in multiple tables. I started off by having these fields as member variables of Java Bean. Then I saw the section (9.1) on 'Dependent Objects' and figure I would extract these fields into one class (i.e. Name.class) and reuse that class in other classes (i.e. Person.class).
I want to be able to perform a search that will select only certain fields out of the Person table (ID, first name, and last name) and base the search on 'last name'. Before I extracted out the Name.class I had the following:
[code]
Criteria criteria = getSession().createCriteria(Person.class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("Key"), "Key")
.add(Projections.property("first"), "first")
.add(Projections.property("last"), "last"));
criteria.add(Expression.eq("last", searchCriteria.getLastName()));
criteria.list();[/code]
I changed my mapping document to be the following, hoping Hibernate would be able to see that "last" is now a property of "Name" and no longer a property of "Person".
[code]Mapping documents:
<class name="eg.Person" table="person">
<id name="Key" column="pid" type="string">
<generator class="uuid.hex"/>
</id>
<component name="Name" class="eg.Name"> <!-- class attribute optional -->
<property name="initial"/>
<property name="first"/>
<property name="last"/>
</component>
</class>
[/code]
Now I get the following error message.
[b]Full stack trace of any exception that occurs:
Caused by: org.hibernate.QueryException: could not resolve property: last
of: eg.Person
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.BasicEntityPersister.toType(BasicEntityPersister.java:1094)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getType(CriteriaQueryTranslator.java:442)
at org.hibernate.criterion.PropertyProjection.getTypes(PropertyProjection.java:36)
at org.hibernate.criterion.AliasedProjection.getTypes(AliasedProjection.java:37)
at org.hibernate.criterion.ProjectionList.getTypes(ProjectionList.java:38)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:297)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:79)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1297)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:299)
[/b]
It appears that Hibernate does not realize that it needs to now map 'last' to the Name class and not the Person class. Is there a way that I need to "tell" Hibernate that Name is now involved?
Thanks.
|