-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to map a Class with properties from multiple tables
PostPosted: Mon Dec 22, 2008 9:50 am 
Newbie

Joined: Mon Dec 22, 2008 9:41 am
Posts: 8
Hi all,

I need to map a Java class called Friend which is made up entirely of properties that come from other tables/mapped classes.

eg
Code:
class Friend {

private String userUuid;
private boolean confirmed;
private String statusMessage;
private Date statusDate;
private byte[] photo;

...
}


where userUuid and confirmed comes from ProfileFriend (PROFILE_FRIEND_T), the status fields come from ProfileStatus (PROFILE_STATUS_T) and the photo comes from a field in PERSON_T. The userUuid is the joining property across all tables, except in the last table it's called something else (agentUuid).

I am not sure how to set out my hbm.xml so that Friend is mapped properly. There is no table that Friend maps directly to.

The reason for this is because of a particular SQL query, I need it to return a list of Friend objects rather than a list of Object[] arrays.

My sql query is this:
Code:
   <sql-query name="getFriendsForUser">
      <return alias="friend" class="Friend"/>
       <![CDATA[
       select PROFILE_FRIENDS_T.FRIEND_UUID as {friend.userUuid}, PROFILE_FRIENDS_T.CONFIRMED as {friend.confirmed}, PROFILE_STATUS_T.MESSAGE as {friend.statusMessage}, PROFILE_STATUS_T.DATE_ADDED as {friend.statusDate}, PERSON_T.JPEG_PHOTO as {friend.photo} from PROFILE_FRIENDS_T left join PROFILE_STATUS_T on PROFILE_FRIENDS_T.FRIEND_UUID=PROFILE_STATUS_T.USER_UUID left join PERSON_T on PROFILE_FRIENDS_T.FRIEND_UUID=PERSON_T.AGENT_UUID where PROFILE_FRIENDS_T.USER_UUID = :userUuid
      union
      .... some more sql.
      ]]>
   </sql-query>



Can someone please help me? Thanks.
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2008 2:01 pm 
Newbie

Joined: Mon Dec 22, 2008 9:41 am
Posts: 8
I may ahve solved this myself. I am looking into using a non managed entity

Quote:
16.1.5. Returning non-managed entities

It is possible to apply a ResultTransformer to native sql queries. Allowing it to e.g. return non-managed entities.

sess.createSQLQuery("SELECT NAME, BIRTHDATE FROM CATS")
.setResultTransformer(Transformers.aliasToBean(CatDTO.class))
This query specified:

the SQL query string

a result transformer

The above query will return a list of CatDTO which has been instantiated and injected the values of NAME and BIRTHNAME into its corresponding properties or fields.


I went down the path of manually parsing the List of Object[]'s, but that was my last resort. I finally found the Transformers and hope they will provide what I want.

Are they more than meets the eye?


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