4

Recentemente comecei a estudar um dos frameworks mais fantásticos que já conheci, o Spring.

Sempre costumo avançar bem nos meus estudos, já estou estudando a integração do Spring com ferramentas de acesso a dados. Estou tentando integrar o Spring com o Hibernate e JPA.

A configuração foi realizada com sucesso, está conseguindo instanciar o EtityManagerFactory e o EntityManager normalmente. O problema é que não estou conseguindo salvar as informações no banco de dados e estou com problemas nas transações que devem ser realizadas pelo Spring.

O maior problema é o seguinte: Quando anoto os métodos dos meus DAOs com @Transactional uma exceção é lançada. Segue a exceção lançada:

Ago 12, 2014 3:43:41 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons Informações: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@779cbd8d: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,personDAO,personService,entityManagerFactory,dataSource,jpaVendorAdapter,jpaDialect,dbUtil,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy Ago 12, 2014 3:43:41 PM org.springframework.orm.jpa.AbstractEntityManagerFactoryBean destroy Informações: Closing JPA EntityManagerFactory for persistence unit 'springJpa' Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDAO' defined in file [/home/arkson/workspace/web/Spring_Jpa/build/classes/br/com/springjpa/PersonDAO.class]: Initialization of bean failed; nested exception is java.lang.IllegalAccessError at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at br.com.springjpa.TestSpringHibernateJpa.main(TestSpringHibernateJpa.java:9) Caused by: java.lang.IllegalAccessError at net.sf.cglib.core.ClassEmitter.setTarget(ClassEmitter.java:45) at net.sf.cglib.core.ClassEmitter.(ClassEmitter.java:37) at net.sf.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:165) at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216) at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145) at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117) at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108) at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104) at net.sf.cglib.proxy.Enhancer.(Enhancer.java:69) at org.springframework.aop.framework.Cglib2AopProxy.createEnhancer(Cglib2AopProxy.java:228) at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:170) at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:476) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1461) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) ... 11 more

Detalhe: Os meus DAOs estão anotados com @Repository com segue:

@Repository
public class PersonDAO {

    @PersistenceContext
    private EntityManager entityManager;

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public EntityManager getEntityManager() {
        return entityManager;
    }

    @Transactional
    public void save(Person person) {
        System.out.println("saving...");
        this.entityManager.persist(person);
        System.out.println("saved...");
    }

}

E segue também o xml do spring:

<?xml version="1.0" encoding="UTF-8"?>
<beans ...>

    <context:annotation-config />
    <context:component-scan base-package="br.com.springjpa" />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="springJpa" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/springbase" />
        <property name="username" value="root" />
        <property name="password" value="rooot" />
    </bean>

    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="MYSQL" />
        <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
    </bean>

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

    <bean id="dbUtil" class="br.com.springjpa.DbUtil">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>
3
  • Cara, Caused by: java.lang.IllegalAccessError at net.sf.cglib.core.ClassEmitter.setTarget(ClassEmitter.java:45). Parece incompatibilidade de biblioteca... Você está usando maven? Pode postar seu pom.xml? Chute alto: problemas com versão do cglib e asm (fonte: sagarkarnati.wordpress.com/2011/09/13/…) Commented 12/08/2014 às 23:16
  • Não estou usando maven. Todas as minhas biblioteas estão no diretório WEB-INF/bin. Commented 13/08/2014 às 2:17
  • Anthony Accioly, era isso mesmo. Baixei as versões mais recentes das bibliotecas e deu tudo certo, foi resolvido. Commented 13/08/2014 às 11:28

1 Resposta 1

2

Transformando meu comentário em resposta.

Pelo stack trace:

 Caused by: java.lang.IllegalAccessError at    
     net.sf.cglib.core.ClassEmitter.setTarget(ClassEmitter.java:45) at 
     net.sf.cglib.core.ClassEmitter.(ClassEmitter.java:37) at 
     net.sf.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:165) at 
     net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at 
     net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216) at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145) at 
     net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117) at 
     net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108) at 
     net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104) at net.sf.cglib.proxy.Enhancer.(Enhancer.java:69) at 
     // ...

Parece que há alguma incompatibilidade de bibliotecas. Em especial, conforme esse artigo, parece que os culpados desse tipo de exceção costuam ser o cglib e o asm. Tente atualizar essas bibliotecas conforme dependências comuns do Spring e Hibernate.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .