Filter condition using attributes defined in base

2019-08-30 09:31发布

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条回答
虎瘦雄心在
2楼-- · 2019-08-30 09:48

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.

查看更多
登录 后发表回答