@Where子句在hibernate连接查询中不起作用 [英] @Where clause does not work inside hibernate join query

查看:560
本文介绍了@Where子句在hibernate连接查询中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个使用@Where注解的实体。第一个是类别;

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

和它有以下关系;

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

,第二个实体是SubCategory;

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

并包含对应的关系;

  @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =CATEGORY_ID)
私人类别类别;

无论何时我调用以下Dao方法;

  @Query(value =从类别类别中选择不同的类别连接获取category.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_
内连接
PUBLIC.t_sub_category subcategor1_
on category0_.id = subcategor1_.category_id
where

category0_.DELETED ='0'

你能告诉我为什么上面的查询缺乏吗?


和subcategor1_.DELETED ='0'

blockquote>

在其中的哪个区块?

解决方案

我刚刚解决了类似的问题在我的项目中。

有可能将@Where注释不仅放在实体上,而且也在你的孩子收集。



根据 javadoc


Where子句添加到元素实体或集合的实体


就你的情况而言, p>

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

请找到类似的问题解决 here 我相信这样的解决方案并不像侵入性使用Hibernate过滤器。这些过滤器默认情况下是禁用的,并且在会话级别上运行,因此每次新会话打开时都可以进行额外的工作,特别是当您的DAO通过抽象如Spring Data工作时

I have 2 entities with @Where annotation. First one is Category;

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

and it has the following relation;

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

and second entity is SubCategory;

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

and contains corresponding relation;

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

Whenever I call the following Dao method;

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

I got the following sql query;

    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' 
        )

Could you please tell me why the above query lacks

and subcategor1_.DELETED = '0'

inside its where block?

解决方案

I have just solved a similar problem in my project.

It is possible to put @Where annotation not only on Entity, but on also on your child collection.

According to the javadoc:

Where clause to add to the element Entity or target entity of a collection

In your case, it would be like :

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

Please find a similar issues resolved here

I believe thus solution is not as invasive compared to using Hibernate Filters.These filters are disabled by default and operate on Session level, thus enabling them each time new Session opens is extra work especially when your DAO works through abstractions like Spring Data

这篇关于@Where子句在hibernate连接查询中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆