使用基类中定义的属性过滤条件 [英] Filter condition using attributes defined in base class
问题描述
我有3类 Base
, Child
和 Other
定义如下:
I have 3 classes Base
, Child
and Other
defined as follows:
@Entity
@Filter(name = "myFilter", condition = "propBase = 'special'")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Base {
private String propBase;
//Getters, Setters
}
@Entity
public class Child extends Base {
private String propChild;
//Getters, Setters
}
@Entity
public class Other {
@Filter(name = "myFilter", condition = "propBase = 'special'")
private Set<Child> myList;
//Getters, Setters
}
假定过滤器是在包级别定义的,因此使用它的所有类都可以看到它.
Assume the filter is defined at the package level so it is visible by all the classes that use it.
使用启用了 myFilter 的会话,我从数据库中检索了 Other
的某些实例.然后,当我尝试访问 myList
集合时,由于该集合被声明为惰性的,因此Hibernate尝试从数据库中获取该集合.但是,在生成的SQL查询中我没有想到的事情:表 Child
的别名用于为 propBase
列添加前缀,并且由于该列不是定义在表 Child
(在 Base
中定义)中,出现以下错误:
Using a session with myFilter enabled, I retrieve some instance of Other
from my database. Then, when I try to access the myList
collection, since the collection is declared as lazy, Hibernate tries to fetch the collection from the database. But, there is something that I didn't expect in the generated SQL query: the alias of table Child
is used to prefix the propBase
column, and since this column is not defined in the table Child
(it is defined in Base
), I get the following error:
错误JDBCExceptionReporter-中的未知列'childAlias.propBase'"where子句"
ERROR JDBCExceptionReporter - Unknown column 'childAlias.propBase' in 'where clause'
根据此线程,它似乎是预期的行为,但我不知道该怎么做.另外,假设这是预期的行为,当条件使用基类中定义的属性时如何使用过滤?
According to this thread, it seems to be the expected behavior but I don't understand how it can be. Also, assuming this is the expected behavior, how to use filtering when the condition uses properties defined in base class?
谢谢
推荐答案
您需要使用sql片段别名- https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#pc-filter-sql-fragment-alias
You need to use sql fragment alias - https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#pc-filter-sql-fragment-alias
这篇关于使用基类中定义的属性过滤条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!