Results 1 to 6 of 6

Thread: Transaction error

  1. #1
    Join Date
    Feb 2011
    Posts
    4

    Default Transaction error

    Hi, I try to use spring for a personal project.
    here is my context.xml
    Code:
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/appli" />
    		<property name="username" value="****" />
    		<property name="password" value="****" />
    	</bean>
    
    	<bean id="entityManagerFactory"		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="persistenceXmlLocation" value="META-INF/persistence.xml"/>
    		<property name="jpaVendorAdapter">
    			<bean id="jpaVendorAdapter"
    				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="database" value="MYSQL" />
    				<property name="showSql" value="true" />
    				<property name="generateDdl" value="true" />
    			</bean>
    		</property>
    	</bean>
    
    	<!--<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>-->
    	
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    
    	<bean
    		class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    	<context:annotation-config />
    
    	<!-- Exception translation bean post processor -->
    	<bean
    		class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    
    	<context:component-scan base-package="fr.appli" />
    
    </beans>
    my persistence.xml
    Code:
    	<persistence-unit name="appliPU" transaction-type="RESOURCE_LOCAL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    		<properties>
    			<!-- Scan for annotated classes and Hibernate mapping XML files -->
    			<!--<property name="hibernate.archive.autodetection" value="class, hbm" />-->
    
    			<property name="hibernate.hbm2ddl.auto" value="create-drop" />
    			<property name="show_sql" value="true" />
    			<property name="dialect" value="org.hibernate.dialect.MySQLDialect" />
    			<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
    		</properties>
    	</persistence-unit>
    </persistence>
    My pom.xml
    Code:
    <dependencies>
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<version>5.1.13</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>jcommon</groupId>
    			<artifactId>jcommon</artifactId>
    			<version>0.9.5</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.ow2.easybeans</groupId>
    			<artifactId>easybeans-jpa-default-toplink-essentials</artifactId>
    			<version>1.2.0-M3</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.8.2</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-core</artifactId>
    			<version>3.0.5.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    			<version>3.0.5.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-test</artifactId>
    			<version>3.0.5.RELEASE</version>
    			<type>jar</type>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-hibernate3</artifactId>
    			<version>2.0.8</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>javax.transaction</groupId>
    			<artifactId>jta</artifactId>
    			<version>1.1</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-expression</artifactId>
    			<version>3.0.5.RELEASE</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-beans</artifactId>
    			<version>3.0.5.RELEASE</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-aop</artifactId>
    			<version>3.0.5.RELEASE</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context-support</artifactId>
    			<version>3.0.5.RELEASE</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-tx</artifactId>
    			<version>3.0.5.RELEASE</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-jdbc</artifactId>
    			<version>3.0.5.RELEASE</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-orm</artifactId>
    			<version>3.0.5.RELEASE</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-api</artifactId>
    			<version>1.6.1</version>
    			<type>jar</type>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-nop</artifactId>
    			<version>1.6.1</version>
    			<type>jar</type>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.hibernate</groupId>
    			<artifactId>hibernate-entitymanager</artifactId>
    			<version>3.4.0.GA</version>
    			<exclusions>
    				<exclusion>
    					<groupId>asm</groupId>
    					<artifactId>asm</artifactId>
    				</exclusion>
    				<exclusion>
    					<groupId>asm</groupId>
    					<artifactId>asm-attrs</artifactId>
    				</exclusion>
    				<exclusion>
    					<groupId>cglib</groupId>
    					<artifactId>cglib</artifactId>
    				</exclusion>
    			</exclusions>
    		</dependency>
    		<dependency>
    			<groupId>javax.persistence</groupId>
    			<artifactId>persistence-api</artifactId>
    			<version>1.0</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>javax</groupId>
    			<artifactId>javaee-api</artifactId>
    			<version>6.0</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.synyx.hades</groupId>
    			<artifactId>org.synyx.hades</artifactId>
    			<version>1.6.3.RELEASE</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>cglib</groupId>
    			<artifactId>cglib-nodep</artifactId>
    			<version>2.1_3</version>
    		</dependency>
    		<dependency>
    			<groupId>commons-dbcp</groupId>
    			<artifactId>commons-dbcp</artifactId>
    			<version>1.4</version>
    		</dependency>
    	</dependencies>
    My abstract test class
    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:spring/context.xml",
    		"classpath:spring/Dao-context.xml" })
    // @TransactionConfiguration(transactionManager = "transactionManager",
    // defaultRollback = true)
    @Transactional
    public abstract class CRUD<E extends AbstractEntity<PK>, PK extends Serializable, I extends GenericDao<E, PK>>
    		implements ICRUD<PK, E, I> {
    
    	@PersistenceContext
    	protected EntityManager entityManager;
    
    	protected Class<E> entityClass;
    
    	protected I dao;
    
    	@SuppressWarnings("unchecked")
    	public CRUD() {
    
    		entityClass = (Class<E>) (((ParameterizedType) getClass().getGenericSuperclass())
    				.getActualTypeArguments()[0]);
    	}
    
    	@Test
    	public void create() {
    
    		E entity = MockFactory.on(entityClass).create();
    		dao.save(entity);
    		Assert.assertTrue(entityManager.contains(entity));
    	}
    
    	@Test
    	public void read() {
    
    		E entity = MockFactory.on(entityClass).createAndPersist(entityManager);
    		E entity2 = dao.readByPrimaryKey(entity.getId());
    		Assert.assertEquals(entity, entity2);
    	}
    ...
    When i lunch my test i got this exception :
    Code:
    java.lang.NoSuchMethodError: org.springframework.transaction.interceptor.TransactionAttribute.getQualifier()Ljava/lang/String;
    when I comment @transactional i got this exception :
    Code:
    java.lang.IllegalArgumentException: The given primaryKey must not be null!
    I can not find solutions on the internet.
    Anyone have à clue ?

  2. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    First exception is because you didn't configure Spring transactions properly, second error is because you try to persist a jpa entity with id property (the one marked with @Id) set to null. I also see a lot of other misconfiguration, you should study both JPA and Spring more carefully.

  3. #3
    Join Date
    Feb 2011
    Posts
    4

    Default

    Hi thank for your response.

    For the second exception, my entities @id are @GeneratedValue(strategy = GenerationType.AUTO)

    when i call persist() method, it should generate an id, without use the flush()?

    for spring, it's the first time i use it, can you tell me what i have missed in my configuration ?

  4. #4
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    my entities @id are @GeneratedValue(strategy = GenerationType.AUTO)
    ok, but do they all map to numeric columns which support id auto generation? Second, you are using mocks instead of real entities, have you tried the test using a real entity? Does it work?

    can you tell me what i have missed in my configuration ?
    well...I suggest you refer to the official reference guide for Spring / Jpa integration. In short, you should configure transactionality using the tx:namespace to be able to effectively use the @Transactional annotation. Second thing is, the configuration you posted can be used for test purposes but if you need transaction support in production environment you should consider switching to container driven jpa support. This in turn would give you automatic JTA support which you should configure as your transactional strategy, again using the tx namespace...it's really not easy concepts and all so please refer to the documentation.

  5. #5
    Join Date
    Feb 2011
    Posts
    4

    Default

    ok, but do they all map to numeric columns which support id auto generation?
    Yes, my jpa configuration create the tables in my schema, and id's columns are in auto increment .

    Second, you are using mocks instead of real entities, have you tried the test using a real entity?
    My mock use real entities, it don't use easyMock or something like.
    Code:
    protected void populate(int seed, Consumption mock) {
    
    		mock.setCost(seed);
    		mock.setDate(new Date(seed));
    		mock.setMileage(seed);
    		mock.setQuantity(seed);
    	}
    I will take a look to the tx:namespace thank.

  6. #6
    Join Date
    Feb 2011
    Posts
    4

    Default

    It's ok i have find my errors. I had some dependencies errors in my pom.xml.

    Thanks for help !

Posting Permissions

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