Announcement Announcement Module
Collapse
No announcement yet.
JPA and Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JPA and Spring

    Hi,
    Im using JPA with spring 2.0. Im not able to persist the object into the database though im able to select the same. Alos, when i try to insert in the database using the primary key already existing it gives me an integrity contraint error which means it tries to insert,. but is not commiting any value.

  • #2
    Please, provide more information: code, mappings, log file, exceptions etc.

    Comment


    • #3
      Application Context
      <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
      <property name="jndiName" value="java:comp/env/jdbc/VikramJFADS" />
      </bean>

      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.TopLinkJ paVendorAdapter">
      <property name="showSql" value="true"/>
      <property name="generateDdl" value="false"/>
      <property name="databasePlatform" value="oracle.toplink.essentials.platform.database .oracle.OraclePlatform"/>
      </bean>
      </property>

      <property name="loadTimeWeaver">
      <bean class="org.springframework.instrument.classloading .oc4j.OC4JLoadTimeWeaver"/>
      </property>
      </bean>

      <bean id="txProxyTemplate" abstract="true"
      class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
      <property name="transactionManager"><ref bean="jfaTransactionManager"/></property>
      <property name="transactionAttributes">
      <props>
      <prop key="save*">PROPAGATION_REQUIRED</prop>
      <prop key="remove*">PROPAGATION_REQUIRED</prop>
      <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
      </props>
      </property>
      </bean>

      <bean id="jfaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
      <property name="entityManagerFactory" ref="entityManagerFactory"/>
      <property name="dataSource" ref="dataSource"/>
      </bean>

      In the DAO I simply write getJpaTemplate().persist(Object)

      Is there any configuration to set commit=true?

      Comment


      • #4
        Fine, but what error you get when trying to persist an object?

        Comment


        • #5
          Thats what I dont even get any exception. But the problem is it does not persist any value in the database. If i try to persist with primary key already in database, it gives me an exception saying that key already exists, which means that it tries to persist atleast, but does not commit the value. Is there any configuration in the server or application context to set commit=true? I am using Oc4j server and oracle database.

          Comment


          • #6
            Enable debug logging in Spring and check if the transaction manager works as you intended. You should see transaction manager log entries. I haven't used
            toplink but try to enable their logging facility and see what's going on there.

            Comment


            • #7
              Hello. I have the exact same problem. We're using EJB3, but for inserting some initial data to the DB we use Spring. We configured it to work with JPA, but data is not actually persisted. We're using Hibernate as the underlying implementation. The same problem occurred while testing the beans with ejb3unit. All the selects worked fine, but the persist didn't. Any help would be greatly appreciated!

              Thanks!

              Comment


              • #8
                Hi. I managed to solve the problem. All I needed was an @Transactional on the dao implementations. It is rather normal. As long as you don't have a transaction, the entity manager cannot perform a commit and flush the data.

                Comment


                • #9
                  well in my case it is not working.

                  Comment


                  • #10
                    Hello. I will give u my configuration and maybe that will help u. Unfortunately, I can only do that on Friday. Sorry.

                    Comment


                    • #11
                      sure, thanx dude.give me the dao and app-context file.

                      Comment


                      • #12
                        Hi. here's the important part from the app context xml:
                        <code>

                        <tx:annotation-driven />
                        <bean id="persistenceUnitManager"
                        class="org.springframework.orm.jpa.persistenceunit .DefaultPersistenceUnitManager">
                        <property name="persistenceXmlLocations">
                        <list>
                        <value>classpathersistence.xml</value>
                        </list>
                        </property>
                        </bean>
                        <bean id="entityManagerFactory"
                        class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
                        <property name="persistenceUnitManager"
                        ref="persistenceUnitManager" />
                        <property name="persistenceUnitName"
                        value="persistence-unit-name" />
                        </bean>
                        <bean id="transactionManager"
                        class="org.springframework.orm.jpa.JpaTransactionM anager">
                        <property name="entityManagerFactory"
                        ref="entityManagerFactory" />
                        </bean>
                        <bean
                        class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor" />

                        </code>

                        And the DAO is an ordinary class. The only thing is that it has @Transactional

                        Hope it helps.

                        Comment


                        • #13
                          Very important!!! Make sure you put <tx:annotation-driven/> in your xml file! Otherwise, the @Transactional will be in vain.

                          Comment


                          • #14
                            Any solution to the above problem

                            I have the very same problem...

                            Comment


                            • #15
                              Use Annotation Driven Transaction Manager

                              Try using the following approach...
                              <bean
                              class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor" />

                              <bean id="catalogService"
                              class="com.mobchannel.store.catalog.service.Catalo gServiceImpl"
                              scope="singleton" />

                              <bean id="dataSource"
                              class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
                              <property name="driverClassName">
                              <value>com.mysql.jdbc.Driver</value>
                              </property>
                              <property name="url">
                              <value>${mobchannel.database.uri}</value>
                              </property>
                              <property name="username">
                              <value>${mobchannel.database.user.username}</value>
                              </property>
                              <!-- Make sure <value> tags are on same line - if they're not,
                              authentication will fail -->
                              <property name="password">
                              <value>${mobchannel.database.user.password}</value>
                              </property>
                              </bean>
                              <bean id="entityManagerFactory"
                              class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
                              <property name="dataSource" ref="dataSource" />
                              <property name="jpaVendorAdapter">
                              <bean
                              class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter">
                              <property name="database" value="MYSQL" />
                              <property name="showSql">
                              <value>${mobchannel.hibernate.showsql}</value>
                              </property>
                              </bean>
                              </property>
                              </bean>
                              <bean id="transactionManager"
                              class="org.springframework.orm.jpa.JpaTransactionM anager">
                              <property name="entityManagerFactory"
                              ref="entityManagerFactory" />
                              </bean>
                              <tx:annotation-driven transaction-manager="transactionManager" />

                              Comment

                              Working...
                              X