简体   繁体   中英

creating HQL query for many to many relationship

How to create Hql query for many to many relation ship.i am tried but not got perfect solution.So help me how i write query

Model Class

@Entity
@Table(name="TBL_STD_USERPROFILE")
public class UserProfile implements Serializable{

    @OrderBy("skillName ASC")
        @ManyToMany(targetEntity=Skills.class,cascade=CascadeType.ALL,fetch=FetchType.EAGER)
        @JoinTable(name="USER_SKILLS",joinColumns=@JoinColumn(name="FLD_USER_ID",referencedColumnName="FLD_USER_ID"),inverseJoinColumns=@JoinColumn(name="FLD_SKILLS_ID",referencedColumnName="FLD_SKILLS_ID"))
        private Set<Skills> skillsList;

}

My Query

String hql = "from  UserProfile u  join u.skillsList s where s.SkillName='Java' and u.Firstname='xyz'"

Skills Pojo class i am just checking skill name. So help me how to write query

public class Skills implements Serializable{

    private static final long serialVersionUID = 1L;
    private Long skillId;
    private String skillName;
    private String skillDesc;
    private Long categoryId;

    public Long getSkillId() {
        return skillId;
    }

    public void setSkillId(Long skillId) {
        this.skillId = skillId;
    }

    public String getSkillName() {
        return skillName;
    }

    public void setSkillName(String skillName) {
        this.skillName = skillName;
    }

    public String getSkillDesc() {
        return skillDesc;
    }

    public void setSkillDesc(String skillDesc) {
        this.skillDesc = skillDesc;
    }

    public Long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }

}

In your query, skillName is not same as in your entity class. Change as following and try.

String hql = "from  UserProfile u  join u.skillsList s where s.skillName='Java' and u.Firstname='xyz'"

Also check Firstname is same as in Userprofile class. This could also be the reason for your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM