Announcement Announcement Module
Collapse
No announcement yet.
greenpages sample persist Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • greenpages sample persist

    Hi,

    I'm trying to extend the greenpages sample application. I want to add a method to store one register in the Listing table. I've been following the getting-started guide and followed the "Applying best practices to the middle tier" chapter, so basically I'm doing use of the transaction manager (org.springframework.orm.jpa.JpaTransactionManager ). I also enabled the AspectJ transaction demarcation, as it's indicated in the guide, and then I annotated the JpaDirectoy to be transactional.

    In the JpaDirectory class I created this method:

    public void add(Listing user){
    em.persist(user);
    }

    where 'em' is the EntityManager (annotated with @PersistenceContext) that Spring injects from the factory.

    The application compiles and executes without problems, but the object it's not persisted in the Database.

    I've been reading the forums and there are a huge amount of posts giving solutions for the same (or similar) problem but I could not find one that helps me. Can someone point me the correct direction?? What am I doing wrong?

    Thank you!

    Regards,
    Victor

  • #2
    Hi,

    I've been reading and I think I have a problem with the transactions configuration because the transactions are not commited.
    I added the following line:
    em.getTransaction().commit();

    in order to commit the transaction after the persist(Obj) call. But then I get this error:

    java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

    Can you help me?
    Thank you.


    Best regards,
    Victor

    Comment


    • #3
      Hi,

      I think the problem with the transactions not commiting is because I didn't defined a transaction proxy, I tried defining it in the module-context.xml as follows:

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <!--
      	Application context definition for GreenPages JPA.
      -->
      <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      		xmlns:p="http://www.springframework.org/schema/p"
      		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-2.5.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:load-time-weaver aspectj-weaving="on" />
      	
      	<bean id="entityManagerFactory"
           	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
           	p:dataSource-ref="dataSource">
           	<property name="jpaVendorAdapter">
               	<bean id="jpaVendorAdapter"
                   	class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"
                   	p:databasePlatform="org.eclipse.persistence.platform.database.HSQLPlatform"
                   	p:showSql="true" />
           	</property>
      	</bean>
      	
      	<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
      		<property name="transactionManager" ref="transactionManager"/>
      		<property name="transactionAttributes">
      			<props>
      				<prop key="save*">PROPAGATION_REQUIRED</prop>
      				<prop key="update*">PROPAGATION_REQUIRED</prop>
      				<prop key="delete*">PROPAGATION_REQUIRED</prop>
      			</props>
      		</property>
      	</bean>
      	
      	<context:annotation-config />
      	<!-- <bean id="directory" class="greenpages.jpa.JpaDirectory" /> -->
      	
      	<bean id="directory" class="greenpages.jpa.JpaDirectory" parent="baseTransactionProxy" />
      	
      	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
           p:entityManagerFactory-ref="entityManagerFactory" />
      
      	<tx:annotation-driven mode="aspectj" />
      	
      	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
      
      	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
      </beans>

      but I'm getting the following error:

      Code:
      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean#0': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'directory' defined in URL [bundleentry://110/META-INF/spring/module-context.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'transactionManager' of bean class [greenpages.jpa.JpaDirectory]: Bean property 'transactionManager' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
      	at java.security.AccessController.doPrivileged(Native Method)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
      	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
      	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
      	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
      	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
      	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
      	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
      	at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:288)
      	at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:145)
      	at com.springsource.server.kernel.dm.ContextPropagatingTaskExecutor$2.run(ContextPropagatingTaskExecutor.java:82)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      	at java.lang.Thread.run(Thread.java:619)

      What am I doing wrong?? Can you point me to some example that explains how it works? Thank you!

      Best regards,
      Victor

      Comment


      • #4
        Hi,

        I solved the initial problem. I left all the greenpages how was configured initially.
        Then in the JpaDirectory implementation I add a new EntityManagerFactory with its setter method:

        Code:
        	private EntityManagerFactory emf;
        
            @PersistenceUnit
            public void setEntityManagerFactory(EntityManagerFactory emf) {
                this.emf = emf;
            }
        and then the add method like this:

        Code:
            public void add(Listing user){
        		EntityManager emReg = this.emf.createEntityManager();
        		try {
        			emReg.getTransaction().begin();
        			emReg.persist(newtab);
        			emReg.getTransaction().commit();
        			return 0;
        		} finally {
        			if(emReg != null)
        				emReg.close();
        		}
            }
        this creates a new EntityManager that it's not shared and can commit the transaction.

        I don't know if it's the optimal solution, but at least it works. If someone is kind enough to point me to the right documentation/examples to understand better how it works, I would be very grateful.
        Thank you very much.

        Regards,
        Victor

        Comment

        Working...
        X