-->
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 to use a NativeNamedQuery join 2 tables
PostPosted: Sat Apr 28, 2007 12:44 am 
Newbie

Joined: Sat Apr 28, 2007 12:32 am
Posts: 1
In the hibernate doc,, there is an "area&night" example, but I don't know how to use that NamedNativeQuery in Java.

@org.hibernate.annotations.NamedNativeQueries( {
@org.hibernate.annotations.NamedNativeQuery(
name="night&areaCached", query ="select night.id as nid, night.night_duration, night.night_date, area.id as aid, "
+ "night.area_id, area.name from Night night, tbl_area area where night.area_id = area.id",
resultSetMapping="joinMapping")
} )

@SqlResultSetMapping(name="joinMapping", entities={
@EntityResult(name="org.hibernate.test.annotations.query.Night", fields = {
@FieldResult(name="id", column="nid"),
@FieldResult(name="duration", column="night_duration"),
@FieldResult(name="date", column="night_date"),
@FieldResult(name="area", column="area_id")
}),
@EntityResult(name="org.hibernate.test.annotations.query.Area", fields = {
@FieldResult(name="id", column="aid"),
@FieldResult(name="name", column="name")
})
}
)

Say in java, I do:
List l = entityManager.createNamedQuery("area&night").getReaultList();
What will be the object type of the result list?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 28, 2007 8:03 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
I dont think that query would run, you would probably need to do:
Code:
select new ObjectWithConstructorToTakeAllTheseArgs(
    night.id as nid,
    night.night_duration,
    night.night_date,
    area.id as aid,
    night.area_id,
    area.name
)
from
    Night night,
    tbl_area area
where
    night.area_id = area.id

And then obviously make a ObjectWithConstructorToTakeAllTheseArgs class.

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 28, 2007 10:16 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
Actually Adam,

You are incorrect and you don't need to do that for the example the poster is asking about. You need to brush up on some of your Hibernate docs :)

Quote:
List l = entityManager.createNamedQuery("area&night").getReaultList();
What will be the object type of the result list?


You will get back a tuple Night and Area objects:

Code:
Query q = s.getNamedQuery( "night&areaCached" );
List result = q.list();
Night night = (Night) ( (Object[]) result.get( 0 ) )[0];
Area area (Area) ( (Object[]) result.get( 0 ) )[1];


If you look at the ResultSet mappings, you'll see that the example returns multiple types in the result set. Whenever Hibernate does this, it comes back as an array of Objects in the order they are defined in the mapping.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


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.