java - 使用 Hibernate Criteria 连接不同列上的三个实体所需的帮助

标签 java hibernate join

更新的问题

我有 3 张 table ,例如

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 的步骤如下:

Step 1: Creating criteria for Category Entity Criteria categoriesCriteria = session.createCriteria(CategoriesEntity.class, "categoryEntity");

Step 2: Creating aliases of SubCategoryEntity and SectionEntity categoriesCriteria.createAlias("categoryEntity.subCategoryEntity", "subCatEntity"); categoriesCriteria.createAlias("subCatEntity.sectionsEntity", "subCatSectionEntity");

Step 3: Set the property in projection list

Step 4: Add Restriction as: categoriesCriteria.add(Restrictions.eq("subCatEntity.id", primCategoryId));

Step 5: Set projection property into CategoryEntity Criteria categoriesCriteria.setProjection(projPropList);

Step 6: Getting result 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 部分;”以及它的所有 getter 和 setter 方法。

2)并使用这个

       @JoinColumn(name="section", insertable=false, updatable=false)
       public Set<SectionEntity> getSection(){
          this.section;
           }

然后尝试运行。我希望它会起作用

关于java - 使用 Hibernate Criteria 连接不同列上的三个实体所需的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25737639/

相关文章:

java - 如何在打开 JFrame 的 Java Applet 中制作按钮?

java - Eclipse 无法创建可运行的 jar - 未选择资源

mysql - mysql连接问题

sql-server - SQL Server 基于最近值连接两个表

java - 如何打印接口(interface)方法

带参数的Java Integer对象实例化

java - 加快 Lucene 索引构建过程

java - 在 Kotlin(和 Gradle)中使用 Hibernate Validator Annotation Processor

java - Hibernate 返回基类的代理

php - Delete from inner join mysql查询-两表多条件