I create a lot of hibernate event listeners, to catch all database events like load, persist, update, delete etc. All events will be caught, but named queries will not be caught by my load even开发者_JAVA百科t listener and I would like to ask, what I need to implement, to catch names query?
Basically the involvement of events is determined by the type of the Event(Listener)s and the way of fetching the data. Although you are right and in fact there obviously is a difference between working with entity queries (EntityManager.find..., Session.load...) and working with HQL queries. But it does not matter if HQL queries are used as named query or as regular query in case of HQL.
To show this I created a small example using Spring Boot, Spring Data JPA and Hibernate because it's easy to work with that combination. The effects are similar to other application architectures using Hibernate.
If we use the loading events for example, the directly loaded Entities are only propagating events of type LoadEvent if you use EntityManager.find / Session.load to receive the Entity. Using HQL (either with Spring Data JPA or plain Hibernate) there are only events of this certain type if the suspicious entities are fetched indirectly as association. Contrary to this, PostLoadEvents are fired in every case.
Here are some simple classes to try this.
InterceptionByEventCandidate (this Entities we want to intercept):
package com.example.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class InterceptionByEventCandidate {
public static final String FETCH_ALL_QUERY = "SELECT c FROM InterceptionByEventCandidate c";
public static final String FETCH_ALL_NAMED_QUERY = "selectAll";
@Id
@GeneratedValue
private long id;
private boolean interceptedOnLoad;
private boolean interceptedOnPostLoad;
public long getId() {
return id;
}
public boolean isInterceptedOnLoad() {
return interceptedOnLoad;
}
public void setInterceptedOnLoad(boolean interceptedOnLoad) {
this.interceptedOnLoad = interceptedOnLoad;
}
public boolean isInterceptedOnPostLoad() {
return interceptedOnPostLoad;
}
public void setInterceptedOnPostLoad(boolean interceptedOnPostLoad) {
this.interceptedOnPostLoad = interceptedOnPostLoad;
}
}
CandidateHost (needed host to demonstrate the association fetching):
package com.example.model;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class CandidateHost {
public static final String FETCH_ALL_QUERY = "SELECT h FROM CandidateHost h JOIN h.candidate c";
public static final String FETCH_ALL_NAMED_QUERY = "selectAllHosts";
@Id
@GeneratedValue
private long id;
@OneToOne(cascade = CascadeType.PERSIST)
private InterceptionByEventCandidate candidate;
public InterceptionByEventCandidate getCandidate() {
return candidate;
}
public void setCandidate(InterceptionByEventCandidate candidate) {
this.candidate = candidate;
}
}
Now we need a proper listener, for convenience purposes we combine both mentioned types of events in one single HqlEventListener:
package com.example.event;
import org.hibernate.HibernateException;
import org.hibernate.event.spi.LoadEvent;
import org.hibernate.event.spi.LoadEventListener;
import org.hibernate.event.spi.PostLoadEvent;
import org.hibernate.event.spi.PostLoadEventListener;
import org.springframework.stereotype.Component;
import com.example.model.InterceptionByEventCandidate;
@Component
public class HqlEventListener implements LoadEventListener, PostLoadEventListener {
private static final long serialVersionUID = -7248393324424903264L;
@Override
public void onLoad(LoadEvent event, LoadType loadType) throws HibernateException {
final Object object = event.getResult();
if (object instanceof InterceptionByEventCandidate) {
((InterceptionByEventCandidate) object).setInterceptedOnLoad(true);
}
}
@Override
public void onPostLoad(PostLoadEvent event) {
final Object object = event.getEntity();
if (object instanceof InterceptionByEventCandidate) {
((InterceptionByEventCandidate) object).setInterceptedOnPostLoad(true);
}
}
}
To tell Hibernate which events we want to handle we need some configuration:
package com.example.event;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.internal.SessionFactoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EntityListenerConfiguration {
@PersistenceUnit
private EntityManagerFactory entityManagerFactory;
@Autowired
private HqlEventListener hqlEventListener;
@PostConstruct
protected void init() {
final SessionFactoryImpl sessionFactory = entityManagerFactory.unwrap(SessionFactoryImpl.class);
final EventListenerRegistry eventListenerRegistry = sessionFactory.getServiceRegistry()
.getService(EventListenerRegistry.class);
eventListenerRegistry.getEventListenerGroup(EventType.LOAD).appendListener(hqlEventListener);
eventListenerRegistry.getEventListenerGroup(EventType.POST_LOAD).appendListener(hqlEventListener);
}
}
To provide and reuse the named queries we store it in a package-info file:
@NamedQueries({
@NamedQuery(name = InterceptionByEventCandidate.FETCH_ALL_NAMED_QUERY, query = InterceptionByEventCandidate.FETCH_ALL_QUERY),
@NamedQuery(name = CandidateHost.FETCH_ALL_NAMED_QUERY, query = CandidateHost.FETCH_ALL_QUERY) })
package com.example.event;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;
import com.example.model.CandidateHost;
import com.example.model.InterceptionByEventCandidate;
And of course some Spring Data JPA Repositories are needed to try out if they show the same behavior.
InterceptionByEventCandidateRepository:
package com.example.repository;
import java.util.Set;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.example.model.InterceptionByEventCandidate;
@Repository
public interface InterceptionByEventCandidateRepository extends CrudRepository<InterceptionByEventCandidate, Long> {
@Query(InterceptionByEventCandidate.FETCH_ALL_QUERY)
@Transactional(readOnly = true)
Set<InterceptionByEventCandidate> findByHQL();
@Query(name = InterceptionByEventCandidate.FETCH_ALL_NAMED_QUERY)
@Transactional(readOnly = true)
Set<InterceptionByEventCandidate> findByNamedHQL();
}
CandidateHostRepository:
package com.example.repository;
import java.util.Set;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.example.model.CandidateHost;
@Repository
public interface CandidateHostRepository extends CrudRepository<CandidateHost, Long> {
@Query(CandidateHost.FETCH_ALL_QUERY)
@Transactional(readOnly = true)
Set<CandidateHost> findByHQL();
@Query(name = CandidateHost.FETCH_ALL_NAMED_QUERY)
@Transactional(readOnly = true)
Set<CandidateHost> findByNamedHQL();
}
That was a lot of example code, and here is a (I have to admit also huge) test to check how the events differ between the strategies:
package com.example.repository;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import com.example.model.CandidateHost;
import com.example.model.InterceptionByEventCandidate;
@RunWith(SpringRunner.class)
@Transactional
@SpringBootTest
public class HqlEventTests {
@Autowired
private CandidateHostRepository candidateHostRepository;
@Autowired
private InterceptionByEventCandidateRepository interceptionByEventCandidateRepository;
@PersistenceContext
private EntityManager entityManager;
@Before
public void setUp() {
final CandidateHost host = new CandidateHost();
final InterceptionByEventCandidate interceptionByEventCandidate = new InterceptionByEventCandidate();
host.setCandidate(interceptionByEventCandidate);
candidateHostRepository.save(host);
entityManager.flush();
entityManager.clear();
}
@Test
public void interceptCandidateAtFindingHostBySpringDataJpaHql() {
final Set<CandidateHost> result = candidateHostRepository.findByHQL();
verifyInterceptions(result.stream().findFirst().get().getCandidate(), true, true);
}
@Test
public void interceptCandidateAtFindingHostBySpringDataJpaNamedHql() {
final Set<CandidateHost> result = candidateHostRepository.findByNamedHQL();
verifyInterceptions(result.stream().findFirst().get().getCandidate(), true, true);
}
@Test
public void interceptCandidateAtFindingHostByHibernateHql() {
final Session session = entityManager.unwrap(Session.class);
final Query query = session.createQuery(CandidateHost.FETCH_ALL_QUERY);
verifyInterceptions(((CandidateHost) query.list().stream().findFirst().get()).getCandidate(), true, true);
}
@Test
public void interceptCandidateAtFindingHostByHibernateNamedHql() {
final Session session = entityManager.unwrap(Session.class);
final Query query = session.getNamedQuery(CandidateHost.FETCH_ALL_NAMED_QUERY);
verifyInterceptions(((CandidateHost) query.list().stream().findFirst().get()).getCandidate(), true, true);
}
@Test
public void interceptCandidateBySpringDataJpaHql() {
final Set<InterceptionByEventCandidate> result = interceptionByEventCandidateRepository.findByHQL();
verifyInterceptions(result.stream().findFirst().get(), false, true);
}
@Test
public void interceptCandidateBySpringDataJpaNamedHql() {
final Set<InterceptionByEventCandidate> result = interceptionByEventCandidateRepository.findByNamedHQL();
verifyInterceptions(result.stream().findFirst().get(), false, true);
}
@Test
public void interceptCandidateByHibernateHql() {
final Session session = entityManager.unwrap(Session.class);
final Query query = session.createQuery(InterceptionByEventCandidate.FETCH_ALL_QUERY);
verifyInterceptions((InterceptionByEventCandidate) query.list().stream().findFirst().get(), false, true);
}
@Test
public void interceptCandidateByHibernateNamedHql() {
final Session session = entityManager.unwrap(Session.class);
final Query query = session.getNamedQuery(InterceptionByEventCandidate.FETCH_ALL_NAMED_QUERY);
verifyInterceptions((InterceptionByEventCandidate) query.list().stream().findFirst().get(), false, true);
}
@Test
public void interceptCandidateByHibernateFind() {
long id = interceptionByEventCandidateRepository.findAll().iterator().next().getId();
entityManager.flush();
entityManager.clear();
final Session session = entityManager.unwrap(Session.class);
final InterceptionByEventCandidate candidate = session.load(InterceptionByEventCandidate.class, id);
verifyInterceptions(candidate, true, true);
}
private void verifyInterceptions(final InterceptionByEventCandidate candidate, final boolean expectedLoadEventState,
final boolean expectedPostLoadEventState) {
assertThat("Expected load state did not match!", candidate.isInterceptedOnLoad(), is(expectedLoadEventState));
assertThat("Expected postload state did not match!", candidate.isInterceptedOnPostLoad(),
is(expectedPostLoadEventState));
}
}
Looking at this tests we can see that there is no difference between named and regular queries. If you worry about another type of events I guess it's easy to extend this.
精彩评论