From 5fc81e081e9b2c90d1a9ebc27db1956afdf356ee Mon Sep 17 00:00:00 2001 From: eugenp Date: Fri, 31 May 2013 23:57:15 +0300 Subject: [PATCH] xml configuration working as well --- .../baeldung/spring/PersistenceJPAConfig.java | 21 +++++++------- .../spring/PersistenceJPAConfigXml.java | 3 +- spring-jpa/src/main/resources/jpaConfig.xml | 28 ++++++++++++------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/spring-jpa/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java b/spring-jpa/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java index 29494084b1..dac41e182b 100644 --- a/spring-jpa/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java +++ b/spring-jpa/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java @@ -7,13 +7,11 @@ import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.jpa.JpaTransactionManager; -import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; @@ -21,7 +19,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; import com.google.common.base.Preconditions; -@Configuration +// @Configuration @EnableTransactionManagement @PropertySource({ "classpath:persistence-mysql.properties" }) @ComponentScan({ "org.baeldung.persistence" }) @@ -36,19 +34,20 @@ public class PersistenceJPAConfig { @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() { - final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); - factoryBean.setDataSource(restDataSource()); - factoryBean.setPackagesToScan(new String[] { "org.baeldung.persistence.model" }); + final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); + em.setDataSource(dataSource()); + em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" }); - final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); - factoryBean.setJpaVendorAdapter(vendorAdapter); - factoryBean.setJpaProperties(additionalProperties()); + final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); + // vendorAdapter.set + em.setJpaVendorAdapter(vendorAdapter); + em.setJpaProperties(additionalProperties()); - return factoryBean; + return em; } @Bean - public DataSource restDataSource() { + public DataSource dataSource() { final DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); diff --git a/spring-jpa/src/main/java/org/baeldung/spring/PersistenceJPAConfigXml.java b/spring-jpa/src/main/java/org/baeldung/spring/PersistenceJPAConfigXml.java index e85510a0c4..98f4f47249 100644 --- a/spring-jpa/src/main/java/org/baeldung/spring/PersistenceJPAConfigXml.java +++ b/spring-jpa/src/main/java/org/baeldung/spring/PersistenceJPAConfigXml.java @@ -1,10 +1,11 @@ package org.baeldung.spring; import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.transaction.annotation.EnableTransactionManagement; -// @Configuration +@Configuration @EnableTransactionManagement @ComponentScan({ "org.baeldung.persistence" }) @ImportResource({ "classpath:jpaConfig.xml" }) diff --git a/spring-jpa/src/main/resources/jpaConfig.xml b/spring-jpa/src/main/resources/jpaConfig.xml index f85811f8ed..31df8d69da 100644 --- a/spring-jpa/src/main/resources/jpaConfig.xml +++ b/spring-jpa/src/main/resources/jpaConfig.xml @@ -1,17 +1,24 @@ - - - + + - + + + + + ${hibernate.hbm2ddl.auto} ${hibernate.dialect} @@ -26,10 +33,11 @@ - - + + - + + \ No newline at end of file