Required Help to join three entity on different columns using Hibernate Criteria

Updated Question

I have 3 tables like

Section (id, title, url)//id is Primary key
Category (id, title)//id is Primary key
SubCategory (id, title)//id is Primary key

Here, I want to join these table as in simple query like

Select * From Category cat, Section se, SubCategory subCat  WHERE
 subCat.id=23456  AND subCat.category=cat.id  AND subCat.section = se.id

How can I achieve the above query using Criteria in Hibernate? Can anyone help me on this?

Adding my question here

My entity Files are as:

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

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

Steps to use Hibernate Criteria as:

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();

My resulting query displaying as:

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=?

But I have required expected query as:

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=?

How Can I achieve this, can anyone help me on this?


In your SubCategoryFile make change like this,

1) remove "private int section;" and all getter and setter method of this.

2) and use this

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

and then try to run. I hope it will work

链接地址: http://www.djcxy.com/p/37068.html

上一篇: 通过使用标准,hibernate连接字段的列条件

下一篇: 必需的帮助使用Hibernate Criteria在不同列上连接三个实体