• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating new object within query using criteria

 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am just learning criteria and i have a requirement for a project I just cant seem to find the solution to in criteria. I have already got it working within HQL but as the query is going to become more dynamic I would really like to move it over to criteria.

My Requirement is this, I have a number of TaxRates and i want to be able to make a new List of classes (TaxRateInfo) which will contain only a few attributes from the a TaxRate object (and a few objects within that object), but i cannot find a way of doing this.

My HQL is as follows:

list = getHibernateTemplate()
.find(
"select new com.apakgroup.systemx.model.rates.TaxRateItem (tr.id, tr.rateType, tr.assetType, tr.taxRate, tr.startDate, tr.endDate, tr.ruleType, tr.orderBy, tr.region.country, tr.region.province) from TaxRate tr);

Is there a way to do this in criteria?

John
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a Projection query within the Criteria object. This is basically what you are looking for. You want to say which fields to include in the "select"

Check out section 15.7 at the following link

http://www.hibernate.org/hib_docs/v3/reference/en/html/querycriteria.html

Mark
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah ok i see, but how do you make a list of objects out of those projections?

Thank you,

John
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found the solution to the problem, its:

Add this onto the criteria (also works with createQuery)





I hope this helps anyone else who may come across the same problem.

Thank you,

John
[ September 21, 2006: Message edited by: John Bartlett ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic