开发者

Writing HQL Query for ManyToOne Association

开发者 https://www.devze.com 2023-02-08 02:09 出处:网络
Hii, I have two entities with bidirectional association. Project.java class Project{ int project_id; @OneToMany(mappedBy=\"project\")

Hii, I have two entities with bidirectional association.

Project.java

class Project{

    int project_id;

    @OneToMany(mappedBy="project")
    private Set<Users> projectsUsers = new HashSet<Users>();

   开发者_如何学Go //getters and setters and other fields

}

Users.java

class Users{ 

    int id;
    int userId;
    int project_id 

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="project_id") 
    private Project project; 

    //getters and setters and other fields

}

I want to write an HQL query which retrieves the list of projects associated with a particular userId.

I was writing some thing like this which didnt work.

from Project P where P.projectsUsers.userId=1

When trying to execute this I am getting a exception "illegal attempt to dereference collection" can anyone of you please help me in solving the problem?

Thanks in Advance

With Regards

Phani Kumar


First of all, "the list of projects associated with a particular userId" doesn't make sense since you have one-to-many/many-to-one relation so that each user can be associated with no more than one project.

The query to retrieve that project is as follows:

SELECT u.project FROM users u WHERE u.userId = ?
0

精彩评论

暂无评论...
验证码 换一张
取 消