为多对多关系创建HQL查询

时间:2014-04-03 11:38:20

标签: mysql spring hibernate hql

如何为多对多关系创建Hql查询。我尝试但没有得到完美的解决方案。所以我帮我怎么写查询

模型类

@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;

}

我的查询

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

技能Pojo课我只是检查技能名称。 那么请教我如何编写查询

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;
    }

}

1 个答案:

答案 0 :(得分:1)

在您的查询中,skillName与您的实体类不同。改变如下并尝试。

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

同时检查Firstname与Userprofile类中的相同。这也可能是您遇到问题的原因。