views:

35

answers:

2

Hello, simple question regarding HQL(Hibernate query language)

so i have user class , that can hold a list of Projects, how do i take this out of the database depending on a username,

this is how i take out my user

 String username = "stephen";
 YFUser user = (YFUser) session.createQuery(
      "select u FROM YFUser u where u.username = :username")
      .setParameter("username", name).uniqueResult();

but i want to take out the list of projects

here is the projects list within the class YFUser(my user class);

how would i query the database to get this list of projects

@Entity
@Table(name = "yf_user_table")
public class YFUser implements Serializable,ILightEntity {

.........
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinTable(name = "YFUSER_JOIN_PROJECT", joinColumns = { 
        @JoinColumn(name = "user_id") }, inverseJoinColumns = { 
        @JoinColumn(name = "project_id") })
private List<Project> projects = new ArrayList<Project>();
+1  A: 
List<Project> projects = (List<Project>) session.createQuery( 
      "select u.projects FROM YFUser u where u.username = :username") 
      .setParameter("username", name).list(); 
axtavt
A: 

Just made the following change in your code.

List <> user= (YFUser) session.createQuery( "select u FROM YFUser u where u.username = :username") .setParameter("username", name).list();

Rupeshit