Integration between Spring and JPA

Asked

Viewed 263 times

4

I recently started studying one of the most amazing frameworks I’ve ever known, Spring.

I always make good progress in my studies, I’m already studying the integration of Spring with data access tools. I’m trying to integrate Spring with Hibernate and JPA.

The configuration was successfully performed, is succeeding in instantiating the EtityManagerFactory and the EntityManager normally. The problem is that I am not able to save the information in the database and I am having problems in the transactions that should be carried out by Spring.

The biggest problem is this: When I write down the methods of my Daos with @Transactional an exception is thrown. Follows the exception thrown:

Aug 12, 2014 3:43:41 pm org.springframework.Beans.factory.support.Defaultsingletonbeanregistry destroySingletons Information: 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 Aug 12, 2014 3:43:41 pm org.springframework.Orm.jpa.Abstractentitymanagerfactorybean Destroy Information: 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(Abstractautowirecapablenfactory.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(Abstractautowirecapablenfactory.java:1461) at org.springframework.Beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(Abstractautowirecapablebeanfactory.java:519) ... 11 more

Detail: My Daos are annotated with @Repository with following:

@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...");
    }

}

It also follows the xml of 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>
  • Dude, Caused by: java.lang.IllegalAccessError at net.sf.cglib.core.ClassEmitter.setTarget(ClassEmitter.java:45). It seems library incompatibility... Are you using Maven? Can you post your pom.xml? High kick: problems with version of cglib and asm (source: http://sagarkarnati.wordpress.com/2011/09/13/java-lang-illegalaccesserror-at-net-sf-cglib-core-classemitter-settargetclassemitter-java47/)

  • I am not using Maven. All my biblioteas are in the WEB-INF/bin directory.

  • Anthony Accioly, that’s right. I downloaded the latest versions of the libraries and everything worked out, it was solved.

1 answer

2


Turning my comment into a response.

For 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 
     // ...

It seems that there is some incompatibility of libraries. In particular, as that article, it seems that the culprits of this kind of exception are the cglib and the asm. Try updating these libraries to common Spring and Hibernate dependencies.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.