-->
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.  [ 7 posts ] 
Author Message
 Post subject: Create new object based on fields in select
PostPosted: Fri Feb 24, 2006 4:46 pm 
Regular
Regular

Joined: Thu Jul 29, 2004 11:55 pm
Posts: 75
Hibernate3.1.1/annotationbeta8

I remember there used to be a way to create a new object from fields during a query, but I can't find any documentation on it any more.

Example. table/class t with fields a,b,c,d

I think it went something like

select e(t.a,t.b) from t as t

and it would return a new object e with fields a,b.

Does anyone know what this and how to do it with the latest hibernate? Also, do you know if it can be dome with criteria? Any help would be greatly appreciated.

chuck


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 24, 2006 6:44 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
I have done something done with SQLQuery, whereby you throw a generic query at the database, and then you map the columns and types manually while looping through the results sets...

eg.

Code:
SQLQuery q=sess.createSQLQuery("select firstnm,lastnm,createdt from person_table");
Iterator itr=q.list().iterator();
Object obj[]=null;
while(itr!=null&&itr.hasNext())
{
    col=(Object[])itr.next();
    System.out.println("firstnm is col[0]:" + col[0]);
    System.out.println("lastnm is col[1]" + col[1]);
    System.out.println("createdt is col[2]" + col[2]);
}


But dynamically instantiating a new class that has all the get/set methods created for it, based on a query is not something I know about.
Guess you'd have to define and compile the class on the fly...not sure.

There is a concept that I read about called "4.4. Dynamic models" noted in the reference.pdf doc that allows you to save dynamically without having to use a POJO class. Also there is "Chapter 18. XML Mapping" (noted as being experimental).
Quote:
You can write queries that retrieve dom4j trees
from the database and have any modification you make to the tree automatically synchronized to the database.


Check out:

http://www.hibernate.org/hib_docs/v3/reference/en/pdf/hibernate_reference.pdf

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 24, 2006 8:02 pm 
Regular
Regular

Joined: Thu Jul 29, 2004 11:55 pm
Posts: 75
I guess it wasn't clear, but e is already defined and compiled. It is not a component though. It is not defined in the mapping anywhere, just at query time.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 24, 2006 8:03 pm 
Regular
Regular

Joined: Thu Jul 29, 2004 11:55 pm
Posts: 75
I am trying to find a way to have hibernate already populate the object instead of having me step through the object array and set the fields and remember the correct order so the code doesn't break.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 24, 2006 8:47 pm 
Regular
Regular

Joined: Thu Jul 29, 2004 11:55 pm
Posts: 75
If you have hibernate in action, it is around page 269, writing reporting queries. I have lost my book (well, actually someone borrowed it and it never got returned).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 6:34 pm 
Regular
Regular

Joined: Thu Jul 29, 2004 11:55 pm
Posts: 75
I finally got my hands on a copy of hibernate in action. It was on page 271. To do this in HQL, you use the select new clause

select new e(a,b)....

It would be great if I could only do this with criteria.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 4:01 am 
Regular
Regular

Joined: Wed Sep 28, 2005 6:45 pm
Posts: 56
deleted due to posting in the wrong thread...


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