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.  [ 4 posts ] 
Author Message
 Post subject: Loading Collection of Strings using SQL query
PostPosted: Fri Oct 24, 2008 9:00 am 
Newbie

Joined: Fri Oct 10, 2008 9:17 am
Posts: 13
Greetings,

Is it at all possible to initialize a collection of string values using a custom sql query?

I have a User object that has a Set of String values, named roles, which I need to populate when the User object is retrieved. However certain conditions in other tables dictate which values will be included in the Set, so I need to to do some custom sql.

Code:
public class User
{
    private Set<String> roles;

    ..getters and setters
}


I have been attempting to map the User roles like so:

Code:
<set name="roles" inverse="true" lazy="false">
   <key/>
   <element type="string"  />
   <loader query-ref="userRoles"/>
</set>


With a (simplified) custom query:
Code:
<sql-query name="userRoles">
   <load-collection alias="role" role="User.roles" />
    select distinct {role.role_name_fk} from user_role role where role.user_name_fk = ?
</sql-query>


From what I can gather the <load-collection> can only work with defined entities, and since I have a Set of strings it won't work for me.

I've been experimenting with different configurations with no luck. Basically I just need to know whether it is possible to achieve this using Hibernate mapping.

Thanks,
Alan.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2008 10:50 am 
Regular
Regular

Joined: Mon Apr 19, 2004 6:54 pm
Posts: 79
Have you try with the 'where' clause:

Code:
<map
.....
    where="arbitrary sql where condition"
.....

http://www.hibernate.org/hib_docs/v3/reference/en-US/html/collections-mapping.html

Christophe


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2008 11:26 am 
Newbie

Joined: Fri Oct 10, 2008 9:17 am
Posts: 13
I actually need to select the roles based on values in other tables, so the sql looks more like this:

Code:

select distinct user_role.role_name_fk from user_role, customer, supplier where

   (
      ( user_role.user_name_fk = ? AND user_role.role_name_fk = 'Customer' AND customer.user_name_fk = ? AND customer.customer_active = 1 )
   OR
      ( user_role.user_name_fk = ? AND user_role.role_name_fk = 'Supplier' AND supplier.user_name_fk = ? AND supplier.supplier_active = 1 )

);



I over simplified the sql in the previous example - didn't want it to be too confusing.

I have a user_role table which matches the users and roles, but which roles are added to the Set in the User object is determined by the 'active' value in separate tables representing those roles.

Is it still possible to populate the Set somehow?


Top
 Profile  
 
 Post subject: Did you get a answer?
PostPosted: Mon Oct 27, 2008 6:31 am 
Beginner
Beginner

Joined: Mon Oct 27, 2008 6:26 am
Posts: 36
Mate, I am facing the same problem.

I need to map a collection of basic types using a sql query. Is there anyway doing this?


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