Problems using Hibernate Filter annotation with JBoss
rafael.gumz Dec 13, 2005 8:38 AMI need to use the Hibernate´s FilterDef and Filter annotations on my EntityBean. I configured the annotations but I couldn´t figure out the right moment to enable it because the EntityManager´s HibernateSession is always a different instance. I´m using a Stateless Session Bean.
For example, when I get the EntityManager in the first time, I get its HibernateSession and enable the filter, and this filter doesn´t exists in the next time I get the same EntityManager in the same SessionBean.
I mean, the HibernateSession that had the filter was lost and I have to enable it again if I want the filter to work in the same EntityManager instance.
The EntityBean and its annotations:
@Entity(access = AccessType.FIELD)
@Table(name = "SOME_OTHER_TABLE")
@FilterDef(name="FilterDomain", parameters={@ParamDef(name="param1" type="string")})
@Filters({
@Filter(name="FilterDomain", condition="RV_MEANING is not null and RV_LOW_VALUE <> :param1")
})
public class Dominio {
...
}
The SessionBean and the accessor method to get the EntityManager:
@Stateless
public abstract class AbstractController {
@PersistenceContext(unitName="MyContext", type=PersistenceContextType.TRANSACTION)
private EntityManager em;
public EntityManager getEM() {
//code to enable the EntityBean´s filters
EMUtil.enableFilters(this.em);
return this.em;
}
}
The Utility class to enable the filters:
public class EMUtil {
public static void enableFilters(EntityManager em) {
Session session = ((HibernateSession)em).getHibernateSession();
Filter filter = session.getEnabledFilter("FilterDomain");
if (filter == null) {
//always get into the if, the filter must be enabled every time.
filter = session.enableFilter("FilterDomain");
filter.setParameter("param1", "A");
}
}
}
I tried to use the javax.ejb.PostConstruct annotation and enabled the filter there, but I get a javax.persistence.TransactionRequiredException that tells me the EntityManager must be access within a transaction. And considering my problem, I believe it would happen the same with the HibernateSession.
Does anyone have an idea or example where I can use the Hibernate´s Filter annotation without enabling it in the HibernateSession every time I need to use the EntityManager?