I'm reading the book "Professional Java for Web Applications - Nicholas S. Williams" The book example has this configuration for 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() ); }
But I have been configuring using XML, I cannot figure out how to translate this to XML only, I have this so far
<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>
Can any one guide me how to convert this?
Thanks
Enable Spring Data JPA by annotating the PersistenceContext class with the @EnableJpaRepositories annotation. Configure the base packages that are scanned when Spring Data JPA creates implementations for our repository interfaces.
If we want to use JPA with MySQL database, we need the mysql-connector-java dependency. We'll also need to define the DataSource configuration. We can do this in a @Configuration class or by using standard Spring Boot properties. Spring Boot will automatically configure a data source based on these properties.
If you want to configure Spring Data JPA by using XML configuration (and use the configuration described in the book), you have to follow these steps:
The application context configuration (applicationContext-persistence.xml) file looks as follows:
<?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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With