我有3个类:
事件
PublicEvent extends Event
PersonalEvent extends Event
我的hibernate映射文件有点像下面这样。我想为 PersonalEvent
添加一个过滤器,并且在加载这个对象之前,我已经启用了这个过滤器。但这不起作用。我的hibernate版本是 4.3.11.Final
。
Event.hbm.xml
< hibernate-mapping>
< class name =org.calendar.Event>
...
< filter name =personalEventAuthorizecondition =person_ID =:personId/>
< / class>
< joined-subclass name =org.calendar.PersonalEventextends =org.calendar.Event>
< key column =idproperty-ref =id/>
...
< / joined-subclass>
< joined-subclass name =org.calendar.PublicEventextends =org.calendar.Event>
< key column =idproperty-ref =id/>
...
< / joined-subclass>
< filter-def name =personEventAuthorize>
< filter-param name =personIdtype =integer/>
< / filter-def>
< / hibernate-mapping>
PersonalEventRepository
@Override
public PersonalEvent load(Long id){
Filter filter = getSession()。enableFilter(personEventAuthorize);
filter.setParameter(personId,getAuthenticatedPersonId());
返回super.loadById(id);
}
生成的SQL查询没有我的过滤器。我的问题是什么?为什么hibernate无法为联合子类启用筛选器?
感谢所有...
。我更新了Hibernate用户指南,使其更加明显。
当您加载时, @Filter
不适用直接实体:
Account account1 = entityManager.find(Account.class,1L);
帐户account2 = entityManager.find(Account.class,2L);
assertNotNull(account1);
assertNotNull(account2);
虽然它适用于使用实体查询(JPQL,HQL,Criteria API)的情况:
账户account1 = entityManager.createQuery(
从账户a中选择一个a.id =:id,
Account.class)
.setParameter(id,1L)
.getSingleResult();
assertNotNull(account1);
尝试{
帐户account2 = entityManager.createQuery(
从帐户a中选择一个帐户a.id =:id,
Account.class)
.setParameter (id,2L)
.getSingleResult();
}
catch(NoResultException expected){
expected.fillInStackTrace();
$ b因此,作为解决方法,使用实体查询(JPQL,HQL,Criteria API)加载实体。
I have 3 classes:
Event
PublicEvent extends Event
PersonalEvent extends Event
my hibernate mapping file is somethings like bellow. i wanna add one filter for PersonalEvent
and before loading this object, i was enabled that's filter. but this is not working. my hibernate version is 4.3.11.Final
.
Event.hbm.xml
<hibernate-mapping>
<class name="org.calendar.Event">
...
<filter name="personalEventAuthorize" condition="person_ID = :personId" />
</class>
<joined-subclass name="org.calendar.PersonalEvent" extends="org.calendar.Event">
<key column="id" property-ref="id" />
...
<many-to-one name="person" column="person_ID" entity-name="org.person.Person" />
</joined-subclass>
<joined-subclass name="org.calendar.PublicEvent" extends="org.calendar.Event">
<key column="id" property-ref="id" />
...
</joined-subclass>
<filter-def name="personEventAuthorize">
<filter-param name="personId" type="integer" />
</filter-def>
</hibernate-mapping>
PersonalEventRepository
@Override
public PersonalEvent load(Long id) {
Filter filter = getSession().enableFilter("personEventAuthorize");
filter.setParameter("personId", getAuthenticatedPersonId());
return super.loadById(id);
}
hibernate generated SQL Query without my filter. what is my problem? why hibernate can not enabled filter for joined-subclass?thanks for all...
解决方案 This is not a bug, it's the intended behavior. I updated the Hibernate User Guide to make it more obvious.
The @Filter
does not apply when you load the entity directly:
Account account1 = entityManager.find( Account.class, 1L );
Account account2 = entityManager.find( Account.class, 2L );
assertNotNull( account1 );
assertNotNull( account2 );
While it applies if you use an entity query (JPQL, HQL, Criteria API):
Account account1 = entityManager.createQuery(
"select a from Account a where a.id = :id",
Account.class)
.setParameter( "id", 1L )
.getSingleResult();
assertNotNull( account1 );
try {
Account account2 = entityManager.createQuery(
"select a from Account a where a.id = :id",
Account.class)
.setParameter( "id", 2L )
.getSingleResult();
}
catch (NoResultException expected) {
expected.fillInStackTrace();
}
So, as a workaround, use the entity query (JPQL, HQL, Criteria API) to load the entity.
这篇关于当通过id加载实体时,Hibernate enableFilter不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!