Problem:
entityManager.unwrap(SessionImplementor.class)
cause no transactional entitymanager available
exception.
Code:
@Component
public class HibernateEventWiring {
@Autowired
private ViewListener listener;
@PersistenceContext(unitName = "config-punit")
private EntityManager entityManager;
@PostConstruct
public void registerListeners() {
SessionFactory sessionFactory = getSessionFactory();
EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry().getService(
EventListenerRegistry.class);
registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(listener);
}
@Transactional
private SessionFactory getSessionFactory() {
// EXCEPTION: No transactional entitymanager available
return entityManager.unwrap(SessionImplementor.class).getFactory();
}
}
javax.transaction.Transactional
(Java EE 7) in place oforg.springframework.transaction.annotation.Transactional
. – Butts