Reputation: 180
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;
}
}
Upvotes: 0
Views: 160
Reputation: 914
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.
Upvotes: 1