persistence work

This commit is contained in:
Eugen 2013-06-03 18:39:05 +03:00
parent 0fd0f22970
commit b51efabeb5
4 changed files with 33 additions and 16 deletions

View File

@ -87,7 +87,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hibernate</groupId> <groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId> <artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version> <version>${hibernate.version}</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -13,8 +13,10 @@ 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.hibernate4.HibernateTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
@ -34,17 +36,21 @@ public class PersistenceConfig {
} }
@Bean @Bean
public LocalSessionFactoryBean sessionFactory() { public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
sessionFactory.setDataSource(restDataSource()); em.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.spring.persistence.model" }); em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory; final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
// vendorAdapter.set
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
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")));
@ -55,11 +61,11 @@ public class PersistenceConfig {
} }
@Bean @Bean
public HibernateTransactionManager transactionManager() { public PlatformTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager(); final JpaTransactionManager transactionManager = new JpaTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject()); transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
return txManager; return transactionManager;
} }
@Bean @Bean
@ -67,7 +73,7 @@ public class PersistenceConfig {
return new PersistenceExceptionTranslationPostProcessor(); return new PersistenceExceptionTranslationPostProcessor();
} }
final Properties hibernateProperties() { final Properties additionalProperties() {
return new Properties() { return new Properties() {
{ {
setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
@ -78,4 +84,5 @@ public class PersistenceConfig {
} }
}; };
} }
} }

View File

@ -0,0 +1,10 @@
# jdbc.X
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true
jdbc.user=tutorialuser
jdbc.pass=tutorialmy5ql
# hibernate.X
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop

View File

@ -6,7 +6,7 @@
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0"> id="WebApp_ID" version="3.0">
<display-name>Spring Security Basic Auth Application</display-name> <display-name>Spring Security REST Application</display-name>
<!-- Spring root --> <!-- Spring root -->
<context-param> <context-param>