Results 1 to 3 of 3

Thread: can't proxy Repository class with @transaction annotation

  1. #1
    Join Date
    May 2012
    Posts
    3

    Default can't proxy Repository class with @transaction annotation

    when i add @Transactional on CountryDAO the problems come.error is: java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy31
    CountryDAO.java
    Code:
    @Transactional(readOnly = true)
    public interface CountryDAO extends JpaRepository<Country, String> {
    	
    	public List<Country> findByCountryId(String countryId); 
    	
    	@Modifying
    	@Transactional
    	@Query("delete from Country c where c.countryId =?1  ")
    	public void deleteCountryById(String id);
    }
    testClass
    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(value = "file:WebRoot/WEB-INF/config_*.xml")
    public class AuditTest {
    	
    
    	@Autowired
    	private AuditorAwareImpl auditorAwareImpl;
    	@Autowired
    	private CountryDAO countryDAO;
    	
    	@Test
    	public void testPersistAuditableMethod(){
    //		Country country = new Country();
    //		country.setCountryId("653");
    //		country.setName("china");
    //		countryDAO.save(country);
    //		System.out.println("currentAuditor:"+auditorAwareImpl.getCurrentAuditor());
    		//System.out.println("countryDao.findByCountryId method====>"+countryDAO.findByCountryId("653"));
    		countryDAO.deleteCountryById("653");
    	}
    }
    configure_jpa.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    	xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    	<context:annotation-config />  
    	<!-- Directory to scan for repository classes -->
    	<jpa:repositories base-package="com.my.repository" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager" query-lookup-strategy="create-if-not-found">
    	</jpa:repositories>
    	
    	<!--   -->
    	<jpa:auditing auditor-aware-ref="auditorAware" />
    	
    	<!--  
    	<jdbc:embedded-database id="dataSource" type="HSQL" />
    	-->
    	<!--  
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
    		<property name="url" value="jdbc:oracle:thin:@10.1.1.104:1521:OEMREP"></property>
    		<property name="username" value="eccn"></property>
    		<property name="password" value="eccn"></property>
    		<property name="maxActive">
    			<value>15</value>
    		</property>
    	</bean>
    	 -->
    	 <!--  -->
    	 <bean id="auditorAware" class="com.my.auditing.AuditorAwareImpl"/>
    	 
    	 
    	 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    		<!-- 
    		<property name="dataSource" ref="dataSource"/>
    		 -->
    	</bean>
    	
    	<!-- 配置JPA实体管理器工厂 -->
    	<bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<!-- -->
    		<property name="persistenceXmlLocation" value="classpath*:persistence.xml" />
    		<!-- 
    		<property name="dataSource" ref="dataSource"/>
    		 -->
    		<!-- 
    			<property name="packagesToScan" value="com.my.repository"/>
    		 -->
    		<property name="persistenceUnitName" value="jpa" />
    		<property name="jpaDialect">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
    		</property>
    		<!-- -->
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="generateDdl" value="true" />
    				<!-- 
    				<property name="database" value="HSQL" />
    				 -->
    			</bean>
    		</property>
    		<!-- -->
    		<!-- 
    		<property name="loadTimeWeaver">
    			<bean
    				class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" />
    		</property>
    		 -->
    	</bean>
    	<!-- -->
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> 
    	
    	 <!-- -->
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    	 
    	 <!-- -->
    	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
    	 
    </beans>
    error:
    Code:
    java.lang.IllegalStateException: Failed to load ApplicationContext
    	
    Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy31
    	at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
    	at net.sf.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
    	at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    	at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    	at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    	at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
    	at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:201)
    	... 56 more
    if no @transaction annotation then no error generates.

  2. #2
    Join Date
    Jun 2011
    Posts
    2

    Default

    Make sure your proxy-target-class is "false" for your tx:annotation-driven. See the documentation of tx:annotation-driven

  3. #3
    Join Date
    Sep 2008
    Posts
    2

    Default

    I am experiencing the same issue. Problem is the project I am working on needs to have proxy-target-class set to true for other reasons. Is there some other workaround that anyone has come up with?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •