2016-03-20 66 views
2

我有2個實體@Where註解。第一個是類別;@Where條款不起作用Hibernate內部聯接查詢

@Where(clause = "DELETED = '0'") 
public class Category extends AbstractEntity 

它與下列關係;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "category") 
private Set<SubCategory> subCategories = Sets.newHashSet(); 

第二實體是子類別;

@Where(clause = "DELETED = '0'") 
public class SubCategory extends AbstractEntity 

並且包含對應關係;

@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "CATEGORY_ID") 
private Category category; 

每當我調用下面的Dao方法;

@Query(value = "select distinct category from Category category join fetch category.subCategories subcategories") 
public List<Category> findAllCategories(); 

我收到以下sql查詢;

select 
     distinct category0_.id as id1_3_0_, 
     subcategor1_.id as id1_16_1_, 
     category0_.create_time as create2_3_0_, 
     category0_.create_user as create3_3_0_, 
     category0_.create_userip as create4_3_0_, 
     category0_.deleted as deleted5_3_0_, 
     category0_.update_time as update6_3_0_, 
     category0_.update_user as update7_3_0_, 
     category0_.update_userip as update8_3_0_, 
     category0_.version as version9_3_0_, 
     category0_.name as name10_3_0_, 
     subcategor1_.create_time as create2_16_1_, 
     subcategor1_.create_user as create3_16_1_, 
     subcategor1_.create_userip as create4_16_1_, 
     subcategor1_.deleted as deleted5_16_1_, 
     subcategor1_.update_time as update6_16_1_, 
     subcategor1_.update_user as update7_16_1_, 
     subcategor1_.update_userip as update8_16_1_, 
     subcategor1_.version as version9_16_1_, 
     subcategor1_.category_id as categor11_16_1_, 
     subcategor1_.name as name10_16_1_, 
     subcategor1_.category_id as categor11_3_0__, 
     subcategor1_.id as id1_16_0__ 
    from 
     PUBLIC.t_category category0_ 
    inner join 
     PUBLIC.t_sub_category subcategor1_ 
      on category0_.id=subcategor1_.category_id 
    where 
     (
      category0_.DELETED = '0' 
     ) 

你能告訴我,爲什麼上面的查詢缺乏

和subcategor1_.DELETED = '0'

它在那裏塊內?

回答

0

這是一個快速回復;

@Where(clause = "DELETED = '0'") 
public class SubCategory extends AbstractEntity 

Where將直接查詢SubCategry時生效。 拿不到刪除子類別使用Hibernate Filtershere

2

我剛纔在我的項目解決了類似的問題,因爲爲例。

它有可能把@Where註解不僅實體,但也是你的孩子收集。

按照javadoc

Where子句中添加元素實體或目標的集合

在你的情況下實體,它會像:

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "category") 
@Where(clause = "DELETED = '0'") 
private Set<SubCategory> subCategories = Sets.newHashSet(); 

請找一個類似的問題解決here

我相信這樣比使用Hibernate Filters.These過濾器默認被禁用,並且在會話級操作,因此,每一個新的會話打開是額外的工作,尤其是當你的DAO致力於通過像春天抽象的時間,使他們的解決方案是不是侵入數據