web-dev-qa-db-fra.com

Comment configurer Spring Data JPA à l'aide de XML

Je lis le livre "Professional Java for Web Applications - Nicholas S. Williams" L'exemple de livre a cette configuration pour Spring Data JPA:

@Bean
public DataSource customerSupportDataSource()
{
    JndiDataSourceLookup lookup = new JndiDataSourceLookup();
    return lookup.getDataSource("jdbc/CustomerSupport");
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
{
    Map<String, Object> properties = new Hashtable<>();
    properties.put("javax.persistence.schema-generation.database.action",
            "none");
    properties.put("hibernate.ejb.use_class_enhancer", "true");

    HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
    adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect");

    LocalContainerEntityManagerFactoryBean factory =
            new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(adapter);
    factory.setDataSource(this.customerSupportDataSource());
    factory.setPackagesToScan("com.wrox.site.entities",
            "com.wrox.site.converters");
    factory.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
    factory.setValidationMode(ValidationMode.NONE);
    factory.setLoadTimeWeaver(this.loadTimeWeaver); // TODO: remove when SPR-10856 fixed
    factory.setJpaPropertyMap(properties);
    return factory;
}

@Bean
public PlatformTransactionManager jpaTransactionManager()
{
    return new JpaTransactionManager(
            this.entityManagerFactoryBean().getObject()
    );
}

Mais j'ai configuré en utilisant XML, je ne peux pas comprendre comment traduire cela en XML uniquement, je l'ai jusqu'à présent

<jee:jndi-lookup id="myDataSource" jndi-name="Java:comp/env/jdbc/test"/>

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

<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="packagesToScan" value="com.test" />
</bean>

Quelqu'un peut-il me guider sur la façon de convertir cela?

Merci

19
Navid

Si vous souhaitez configurer Spring Data JPA à l'aide de la configuration XML (et utiliser la configuration décrite dans le livre), vous devez suivre ces étapes:

  1. Configurez le bean de source de données.
  2. Configurez le bean d'usine du gestionnaire d'entités.
  3. Configurez le bean du gestionnaire de transactions.
  4. Activez la gestion des transactions basée sur les annotations.
  5. Configurez Spring Spring Data JPA.

Le fichier de configuration du contexte d'application (applicationContext-persistence.xml) se présente comme suit:

<?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:jpa="http://www.springframework.org/schema/data/jpa"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">

  <!-- Configure the data source bean -->
  <jee:jndi-lookup id="dataSource" jndi-name="Java:comp/env/jdbc/CustomerSupport"/>

  <!-- Create default configuration for Hibernate -->
  <bean id="hibernateJpaVendorAdapter" 
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>

  <!-- Configure the entity manager factory bean -->
  <bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
    <!-- Set JPA properties -->
    <property name="jpaProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
        <prop key="javax.persistence.schema-generation.database.action">none</prop>
        <prop key="hibernate.ejb.use_class_enhancer">true</prop>
      </props>
    </property>
    <!-- Set base package of your entities -->
    <property name="packagesToScan" value="foo.bar.model"/>
    <!-- Set share cache mode -->
    <property name="sharedCacheMode" value="ENABLE_SELECTIVE"/>
    <!-- Set validation mode -->
    <property name="validationMode" value="NONE"/>
  </bean>

  <!-- Configure the transaction manager bean -->
  <bean id="transactionManager" 
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
  </bean>

  <!-- Enable annotation driven transaction management -->
  <tx:annotation-driven/>

  <!-- 
    Configure Spring Data JPA and set the base package of the 
    repository interfaces 
  -->
  <jpa:repositories base-package="foo.bar.repository"/>
</beans>
32
pkainulainen
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.thought.service.*" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="showSql" value="false" />
            <property name="database" value="MYSQL" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        </props>
    </property>
</bean>

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

<bean id="transactionSupport" class="com.thought.service.common.database.TransactionSupport" />

<bean id="transactionTemplate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="proxyInterfaces">
        <list>
            <value>com.thought.service.common.database.TransactionTemplate</value>
        </list>
    </property>
    <!-- <property name="proxyTargetClass" value="false" />   -->
    <property name="transactionManager" ref="transactionManager" />
    <property name="target" ref="transactionSupport" />

    <property name="transactionAttributes">
        <props>
            <prop key="save">PROPAGATION_REQUIRES_NEW,-Java.lang.Exception</prop>
        </props>
    </property>
</bean>

classe publique TransactionSupport implémente TransactionTemplate {

public void save(PersistenceHandler persistanceHandler, ModelTemplate model) throws Exception {
    persistanceHandler.save(model);
}

}

interface publique TransactionTemplate {

public void save(PersistenceHandler persistanceHandler, ModelTemplate model) throws Exception;

}

classe abstraite publique PersistenceHandler {

/**
 * Method to implement the actual transaction controlled database operations
 * @param model {@code ModelTemplate}
 * 
 * @throws Exception
 */
public abstract void save(ModelTemplate model) throws Exception;

}

0
Babu Selvam