Como configurar EJB 3.1 e Hibernate JPA com Hibernate Envers

1 resposta
D

Tenho um projeto com JSF 2, EJB 3.1, Hibernate JPA rodando no JBoss 7. Preciso implementar auditoria nesse projeto. Então gostaria de usa o Hibernate Envers, porém estou com uns problema de configuração. Adicionei a lib hibernate-envers-4.1.4.Final.jar em meu projeto.

No arquivo persistence.xml tenho o seguinte código:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
	<persistence-unit name="View">
	    <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <jta-data-source>java:/Contabil_DS</jta-data-source>
        	<class>model.Cliente</class>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update"/>

		   <!-- other hibernate properties -->
		
		   <property name="hibernate.ejb.event.post-insert"
		             value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" />
		   <property name="hibernate.ejb.event.post-update"
		             value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" />
		   <property name="hibernate.ejb.event.post-delete"
		             value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" />
		   <property name="hibernate.ejb.event.pre-collection-update"
		             value="org.hibernate.envers.event.AuditEventListener" />
		   <property name="hibernate.ejb.event.pre-collection-remove"
		             value="org.hibernate.envers.event.AuditEventListener" />
		   <property name="hibernate.ejb.event.post-collection-recreate"
		             value="org.hibernate.envers.event.AuditEventListener" />
		</properties>        
	</persistence-unit>
</persistence>
Está gerando o seguinte erro ao executar o projeto:
10:45:34,608 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-4) MSC00001: Failed to start service jboss.persistenceunit."AplicacaoEAR.ear/View.war#View": org.jboss.msc.service.StartException in service jboss.persistenceunit."AplicacaoEAR.ear/View.war#View": Failed to start service
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) [rt.jar:1.6.0_32]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.6.0_32]
	at java.lang.Thread.run(Unknown Source) [rt.jar:1.6.0_32]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: View] Unable to build EntityManagerFactory
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)
	at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162)
	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85)
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
	... 3 more
Caused by: org.hibernate.HibernateException: Could not instantiate requested listener [org.hibernate.envers.event.AuditEventListener]
	at org.hibernate.ejb.event.JpaIntegrator.instantiate(JpaIntegrator.java:260)
	at org.hibernate.ejb.event.JpaIntegrator.integrate(JpaIntegrator.java:137)
	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:294)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
	at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84)
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
	... 9 more
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.envers.event.AuditEventListener]
	at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:141)
	at org.hibernate.ejb.event.JpaIntegrator.instantiate(JpaIntegrator.java:257)
	... 14 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.envers.event.AuditEventListener
	at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$1.findClass(ClassLoaderServiceImpl.java:99)
	at java.lang.ClassLoader.loadClass(Unknown Source) [rt.jar:1.6.0_32]
	at java.lang.ClassLoader.loadClass(Unknown Source) [rt.jar:1.6.0_32]
	at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138)
	... 15 more

1 Resposta

T

Parece que a classe “org.hibernate.envers.event.AuditEventListener” não está no classpath.

Só por curiosidade, já que vc está usando Jboss, EJB3.1 etc, porquê vc não usa os “Lifecycle Callbacks” do JPA?

http://openjpa.apache.org/builds/1.1.0/docs/jpa_overview_pc_callbacks.html
http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/listeners.html

[]s,
Thiago

Criado 26 de junho de 2012
Ultima resposta 26 de jun. de 2012
Respostas 1
Participantes 2