-->
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: Hibernate ResultTransformer and hibernate relations
PostPosted: Tue Nov 15, 2011 6:35 am 
Newbie

Joined: Sun Oct 30, 2011 5:43 am
Posts: 13
i have Employee , Role , Department domains

- Employee , Role have many to many relationship.
- Employee , Department have many to one relationship.



Code:
@ManyToOne(cascade = CascadeType.ALL)
           @JoinColumn(name = "fk_department_id")
           private Department department;
   
       @ManyToMany(fetch = FetchType.EAGER)
       @JoinTable(name = "employee_role", joinColumns = { @JoinColumn(name = "employee_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
       private Set<Role> roles = new HashSet<Role>(0);



-Requirement: get all users with role admin:

so here's what i am trying to do:

Code:
List<Employee> employees = getCurrentSession()
                .createSQLQuery(
                      "select"
                            + e.id as id,e.first_name as firstName,e.password as password
                            + "from employee e,employee_role er,role r where e.employee_id=er.employee_id and er.role_id=r.role_id and r.name='ROLE_ADMIN' ")
                .setResultTransformer(Transformers.aliasToBean(Employee.class))
                .list();


Problem: i can get all the properties easily as in above query, except for the Department property(there's column fk_department_id in employee table) and the roles property (there's a join table employee_role), any ideas how to get them with the query and set them in the bean with ResultTransFormer or i will have to do that in a separate queries and set each property separately ?


Top
 Profile  
 
 Post subject: Re: Hibernate ResultTransformer and hibernate relations
PostPosted: Wed Nov 23, 2011 3:56 am 
Newbie

Joined: Wed Nov 23, 2011 3:05 am
Posts: 3
why not use hql?

Query query=getSession().createQuery("select e from Employee e join e.roles r where r.name='ROLE_ADMIN'");
List<Employee> result=query.list();

_________________
上帝啊,带我走吧。


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.