xml configuration working as well
This commit is contained in:
parent
9d86ea24a0
commit
5fc81e081e
@ -7,13 +7,11 @@ import javax.sql.DataSource;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
@ -21,7 +19,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
@Configuration
|
// @Configuration
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
@PropertySource({ "classpath:persistence-mysql.properties" })
|
@PropertySource({ "classpath:persistence-mysql.properties" })
|
||||||
@ComponentScan({ "org.baeldung.persistence" })
|
@ComponentScan({ "org.baeldung.persistence" })
|
||||||
@ -36,19 +34,20 @@ public class PersistenceJPAConfig {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
|
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
|
||||||
final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
factoryBean.setDataSource(restDataSource());
|
em.setDataSource(dataSource());
|
||||||
factoryBean.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||||
|
|
||||||
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
factoryBean.setJpaVendorAdapter(vendorAdapter);
|
// vendorAdapter.set
|
||||||
factoryBean.setJpaProperties(additionalProperties());
|
em.setJpaVendorAdapter(vendorAdapter);
|
||||||
|
em.setJpaProperties(additionalProperties());
|
||||||
|
|
||||||
return factoryBean;
|
return em;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource restDataSource() {
|
public DataSource dataSource() {
|
||||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package org.baeldung.spring;
|
package org.baeldung.spring;
|
||||||
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.ImportResource;
|
import org.springframework.context.annotation.ImportResource;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
// @Configuration
|
@Configuration
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
@ComponentScan({ "org.baeldung.persistence" })
|
@ComponentScan({ "org.baeldung.persistence" })
|
||||||
@ImportResource({ "classpath:jpaConfig.xml" })
|
@ImportResource({ "classpath:jpaConfig.xml" })
|
||||||
|
@ -1,17 +1,24 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||||
xmlns:context="http://www.springframework.org/schema/context"
|
xsi:schemaLocation="
|
||||||
xsi:schemaLocation="
|
|
||||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
|
||||||
|
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
|
||||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
|
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
|
||||||
|
|
||||||
<context:property-placeholder location="classpath:persistence-mysql.properties" />
|
<context:property-placeholder location="classpath:persistence-mysql.properties" />
|
||||||
|
|
||||||
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
|
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||||
<property name="dataSource" ref="dataSource" />
|
<property name="dataSource" ref="dataSource" />
|
||||||
<property name="packagesToScan" value="org.baeldung.persistence.model" />
|
<property name="packagesToScan" value="org.baeldung.persistence.model" />
|
||||||
<property name="hibernateProperties">
|
<property name="jpaVendorAdapter">
|
||||||
|
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
|
||||||
|
<!-- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
|
||||||
|
<property name="generateDdl" value="${jpa.generateDdl}" />
|
||||||
|
<property name="databasePlatform" value="${persistence.dialect}" />
|
||||||
|
</bean> -->
|
||||||
|
</property>
|
||||||
|
<property name="jpaProperties">
|
||||||
<props>
|
<props>
|
||||||
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
|
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
|
||||||
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
|
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
|
||||||
@ -26,9 +33,10 @@
|
|||||||
<property name="password" value="${jdbc.pass}" />
|
<property name="password" value="${jdbc.pass}" />
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
|
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||||
<property name="sessionFactory" ref="sessionFactory" />
|
<property name="entityManagerFactory" ref="myEmf" />
|
||||||
</bean>
|
</bean>
|
||||||
|
<tx:annotation-driven />
|
||||||
|
|
||||||
<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
|
<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user