2017-02-11 148 views
3

加載實體不工作時,我有3類:休眠enableFilter通過ID

  • Event
  • PublicEvent extends Event
  • PersonalEvent extends Event

我的Hibernate映射文件就像波紋管出頭。我想爲PersonalEvent添加一個過濾器,並在加載此對象之前,我已啓用這是過濾器。但這不起作用。我的休眠版本是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生成的SQL查詢沒有我的過濾器。我的問題是什麼?爲什麼hibernate無法爲聯合子類啓用篩選器? 感謝所有...

回答

2

This is not a bug, it's the intended behavior。我更新了Hibernate用戶指南,使其更加明顯。

Account account1 = entityManager.find(Account.class, 1L); 
Account account2 = entityManager.find(Account.class, 2L); 

assertNotNull(account1); 
assertNotNull(account2); 

雖然它適用,如果你使用實體查詢(JPQL,HQL,標準API):

@Filter當您直接加載實體不適

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(); 
} 

所以,作爲解決方法,使用實體查詢(JPQL,HQL,Criteria API)來加載實體。

+0

感謝您的重播。在現在我使用這種方法.. –

+0

這個問題似乎只存在於'joined-subclass' ..! –