NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

integration JPA+Spring

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
rimwimy



Joined: 06 Aug 2012
Posts: 2

PostPosted: Mon Aug 06, 2012 2:05 pm    Post subject: integration JPA+Spring Reply with quote

hello,
please i need help in integration of spring witth JPA, what are the steps?
this what i did in
persitence.xml:
Code:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="RegiePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>Regie</jta-data-source>
    <properties>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

Application-Context
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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 
    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />
 
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}" /-->
 
    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
 
 
  <tx:annotation-driven transaction-manager="transactionManager"/>
 
  <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
        <property name="dataSource" ref="dataSource" />         
        <property name="persistenceUnitName" value="RegiePU" />
        <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="true"/>
            <property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
        </bean>
        </property>
        <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
        </property>
        <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver" />
        </property>         
 
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf" />
</bean>
 
    <bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 
      <property name="driverClassName">
         <value>oracle.jdbc.OracleDriver</value>
      </property>
      <property name="url">
         <value>jdbc:oracle:thin:@127.0.0.1:l521:XE</value>
      </property>
      <property name="username">
         <value>rim</value>
      </property>
                <property name="password">
         <value>rim</value>
      </property>
 
   </bean>
  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="dataSource" ref="dataSource"/>
  </bean>
 
 
 
    <bean id="RubriqueDAO" class="Dao.RubriqueDAO"/>
</beans>

Ma classe RubriqueDAO:
Code:
public class RubriqueDAO extends JpaDaoSupport  {
 @PersistenceContext private EntityManager em;
    public RubriqueDAO() {
    }
@Transactional
    public void persist(Rubrique guest) {
 
      try{
          em.persist(guest);
      }
      catch(Exception e){
          System.out.println(e.getMessage());
 
      }

Ma classe main:
Code:
RubriqueDAO r=new RubriqueDAO();
                Rubrique r1=new Rubrique();
                r1.setCode("123");
                r.save(r1);
Back to top
rimwimy



Joined: 06 Aug 2012
Posts: 2

PostPosted: Thu Aug 09, 2012 11:47 am    Post subject: Reply with quote

please this is didn't work with me it gave an error without any nameor exception in my RubriqueDAO in line with em.persist()
please help me
thks
Back to top
ken.ganfield



Joined: 18 Aug 2008
Posts: 31

PostPosted: Thu Aug 09, 2012 12:14 pm    Post subject: Re: integration JPA+Spring Reply with quote

Are you sure that you want the transaction type in your persistence unit
set to RESOURCE_LOCAL?
I would think that you would want to set it to JTA if you are using JPA.

On 0608//12 16:05 , rimwimy wrote:
Quote:
hello,
please i need help in integration of spring witth JPA, what are the steps?
this what i did in
persitence.xml:

Code:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="RegiePU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>Regie</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>


Application-Context

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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!--bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" /-->

<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->


<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:persistence.xml" />
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="RegiePU" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
</bean>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver" />
</property>

</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName">
<value>oracle.jdbc.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@127.0.0.1:l521:XE</value>
</property>
<property name="username">
<value>rim</value>
</property>
<property name="password">
<value>rim</value>
</property>

</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>



<bean id="RubriqueDAO" class="Dao.RubriqueDAO"/>
</beans>


Ma classe RubriqueDAO:

Code:
public class RubriqueDAO extends JpaDaoSupport {
@PersistenceContext private EntityManager em;
public RubriqueDAO() {
}
@Transactional
public void persist(Rubrique guest) {

try{
em.persist(guest);
}
catch(Exception e){
System.out.println(e.getMessage());

}


Ma classe main:

Code:
RubriqueDAO r=new RubriqueDAO();
Rubrique r1=new Rubrique();
r1.setCode("123");
r.save(r1);





Back to top
jash08



Joined: 11 Aug 2012
Posts: 2

PostPosted: Wed Aug 15, 2012 4:06 am    Post subject: Reply with quote

hi,
You should ask the support team....
sell gw2 gold


Last edited by jash08 on Thu Aug 30, 2012 9:47 am; edited 1 time in total
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo