Filter condition using attributes defined in base class

喜你入骨 提交于 2019-12-25 09:39:08

问题


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.

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:

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?

Thanks


回答1:


I am using Hibernate 4.1 and had the same problem. This explains how to tell Hibernate which entity class your condition is actually defined on. Adding aliases to the @Filter annotation fixed the problem for me.

In JPQL select statements, resolving attributes names through inheritance worked directly, BTW.



来源:https://stackoverflow.com/questions/14642671/filter-condition-using-attributes-defined-in-base-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!