-->
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.  [ 3 posts ] 
Author Message
 Post subject: How do I create a projection based on a "dependent obje
PostPosted: Tue Nov 29, 2005 5:33 pm 
Newbie

Joined: Thu Nov 10, 2005 12:35 pm
Posts: 6
[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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 7:05 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 2:15 pm
Posts: 66
Take a look in my query:

"from DescricaoMaterial as descMat where descMat.class = DescricaoMaterial"

try somethink like this:

"from eg.Person as person where person.getName.getLast() = ..."

You should use session.createQuery() to create a query.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 11:41 am 
Newbie

Joined: Thu Nov 10, 2005 12:35 pm
Posts: 6
ronaldorezende,

Thanks for help, but the issue is with the select statement being generated from the Projection.

The where clause seems to be working just fine.

Thanks.


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