我在jsf中设计了类别和子类别表单。当我选择任何类别时,与此类别相关的子类别将显示在下拉列表中。我有一个问题如何编写hql文件。类别和子类别依赖于onetomany,反之亦然。
以下是子类别dao。
public List getAllSubCategoriesSelectItemName(String name) {
// TODO Auto-generated method stub
Session session = HibernateUtil.getSessionFactory().openSession();
try {
List<Category> liste = session.createQuery("Select S.SubName from SubCategory S left join SC.category scc where scc.name='"+name+"'").list();
return liste;
} catch (Exception e) {
e.printStackTrace();
}finally {
session.close();
}
return null;
}
@Entity
@Table(name="CATEGORY",schema="DEMO")
public class Category implements Serializable{
@Id
@SequenceGenerator(name="catseq",sequenceName="seqCatSEQ",allocationSize=1)
@GeneratedValue(generator="catseq",strategy=GenerationType.SEQUENCE)
@Column(name="ID")
private Integer id;
@Column(name="NAME")
private String name;
@Column(name="CAT_DESC")
private String catDesc;
@OneToMany(cascade = { CascadeType.PERSIST },mappedBy="category")
private Set<SubCategory> subcategories = new HashSet();
public Category() {
super();
// TODO Auto-generated constructor stub
}
public Category(String name, String catDesc) {
super();
this.name = name;
this.catDesc = catDesc;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCatDesc() {
return catDesc;
}
public void setCatDesc(String catDesc) {
this.catDesc = catDesc;
}
public Set<SubCategory> getSubcategories() {
return subcategories;
}
public void setSubcategories(Set<SubCategory> subcategories) {
this.subcategories = subcategories;
}
}
package entities;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="SUBCATEGORY",schema="DEMO")
public class SubCategory implements Serializable{
@Id
@SequenceGenerator(name="subcatseq",sequenceName="seqSubCatSEQ",allocationSize=1)
@GeneratedValue(generator="subcatseq",strategy=GenerationType.SEQUENCE)
@Column(name="ID")
private Integer id;
@Column(name="SUBNAME")
private String SubName;
@Column(name="SUBNAME_DESC")
private String SubNameDes;
@ManyToOne(cascade={CascadeType.PERSIST,CascadeType.REMOVE})
@JoinColumn(name = "CAT_ID")
private Category category;
@ManyToMany(cascade=CascadeType.PERSIST,mappedBy="subcategories")
Set<Product> products = new HashSet<Product>();
public SubCategory() {
super();
// TODO Auto-generated constructor stub
}
public SubCategory(String subName, String subNameDes) {
super();
SubName = subName;
SubNameDes = subNameDes;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSubName() {
return SubName;
}
public void setSubName(String subName) {
SubName = subName;
}
public String getSubNameDes() {
return SubNameDes;
}
public void setSubNameDes(String subNameDes) {
SubNameDes = subNameDes;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
答案 0 :(得分:0)
Select S.SubName from SubCategory S left join SC.category
该查询中没有SC
别名。唯一的现有别名是S
。所以SC.category
不正确。