必需的帮助使用Hibernate Criteria在不同列上连接三个实体
更新问题
我有3个表
Section (id, title, url)//id is Primary key
Category (id, title)//id is Primary key
SubCategory (id, title)//id is Primary key
在这里,我想加入这些表格,就像在简单的查询中一样
Select * From Category cat, Section se, SubCategory subCat WHERE
subCat.id=23456 AND subCat.category=cat.id AND subCat.section = se.id
我如何使用Hibernate中的Criteria实现上述查询? 谁可以帮我这个事?
在这里添加我的问题
我的实体文件如下:
@Entity
@Table(name="section")
public class SectionEntity{
private int id;
private String title;
//Getter & setter method
}
@Entity
@Table(name="category")
public class CategoryEntity{
private int id;
private String title;
private Set<SubCategoryEntity> subCategory;
//Getter & setter method
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name="category", insertable=false, updatable=false)
public Set<SubCategoryEntity> getSubCategory(){
return this.subCategory;
}
}
更新了SubCategoryEntity
@Entity
@Table(name="subcategory")
public class SubCategoryEntity{
private int id;
private String title;
private Set<SectionEntity> sectionEntity;
//Getter & setter method
@OneToMany(fetch=FetchType.LAZY)
@JoinColumn(name="id", insertable=false, updatable=false)
public Set<SectionEntity> getSectionEntity(){
this.section;
}
}
使用Hibernate Criteria的步骤如下:
第1步:为类别实体标准类别创建标准Criteria = session.createCriteria(CategoriesEntity.class,“categoryEntity”);
第2步:创建SubCategoryEntity和SectionEntity的别名categoriesCriteria.createAlias(“categoryEntity.subCategoryEntity”,“subCatEntity”); categoriesCriteria.createAlias(“subCatEntity.sectionsEntity”,“subCatSectionEntity”);
第3步:在投影列表中设置属性
第4步:添加限制为:categoriesCriteria.add(Restrictions.eq(“subCatEntity.id”,primCategoryId));
第5步:将投影属性设置为CategoryEntity Criteria categoriesCriteria.setProjection(projPropList);
第6步:获取结果categoriesCriteria.list();
我的结果查询显示为:
select this_.id as y0_, this_.title as y1_, this_.sef_url as y2_, subcatenti1_.id as y3_, subcatenti1_.title as y4_, subcatenti1_.sef_url as y5_
from jos_categories this_
inner join jos_subcategories subcatenti1_ on this_.id=subcatenti1_.category
inner join jos_sections subcatsect2_ on subcatenti1_.id=subcatsect2_.id
where subcatenti1_.id=?
但我要求预期的查询为:
select this_.id as y0_, this_.title as y1_, this_.sef_url as y2_, subcatenti1_.id as y3_, subcatenti1_.title as y4_, subcatenti1_.sef_url as y5_
from jos_categories this_
inner join jos_subcategories subcatenti1_ on this_.id=subcatenti1_.category
inner join jos_sections subcatsect2_ on subcatenti1_.section=subcatsect2_.id
where subcatenti1_.id=?
我怎样才能做到这一点,任何人都可以帮助我呢?
在你的SubCategoryFile中改变像这样,
1)删除“private int section”; 以及这个的所有getter和setter方法。
2)并使用它
@JoinColumn(name="section", insertable=false, updatable=false)
public Set<SectionEntity> getSection(){
this.section;
}
然后尝试运行。 我希望它会起作用
链接地址: http://www.djcxy.com/p/37067.html上一篇: Required Help to join three entity on different columns using Hibernate Criteria