|
| 1 | +package org.rest.spring; |
| 2 | + |
| 3 | +import java.util.Properties; |
| 4 | + |
| 5 | +import org.apache.commons.dbcp.BasicDataSource; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.orm.hibernate3.HibernateTransactionManager; |
| 10 | +import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean; |
| 11 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 12 | + |
| 13 | +@Configuration |
| 14 | +@EnableTransactionManagement |
| 15 | +public class PersistenceConfig{ |
| 16 | + |
| 17 | + @Value( "${driverClassName}" ) |
| 18 | + private String driverClassName; |
| 19 | + |
| 20 | + @Value( "${url}" ) |
| 21 | + private String url; |
| 22 | + |
| 23 | + @Value( "${hibernate.dialect}" ) |
| 24 | + String hibernateDialect; |
| 25 | + |
| 26 | + @Value( "${hibernate.show_sql}" ) |
| 27 | + String hibernateShowSql; |
| 28 | + |
| 29 | + @Value( "${hibernate.hbm2ddl.auto}" ) |
| 30 | + String hibernateHbm2ddlAuto; |
| 31 | + |
| 32 | + // beans |
| 33 | + |
| 34 | + @Bean |
| 35 | + public BasicDataSource restDataSource(){ |
| 36 | + final BasicDataSource basicDataSource = new BasicDataSource(); |
| 37 | + basicDataSource.setDriverClassName( this.driverClassName ); |
| 38 | + basicDataSource.setUrl( this.url ); |
| 39 | + basicDataSource.setUsername( "restUser" ); |
| 40 | + basicDataSource.setPassword( "restmy5ql" ); |
| 41 | + |
| 42 | + return basicDataSource; |
| 43 | + } |
| 44 | + |
| 45 | + @Bean |
| 46 | + public AnnotationSessionFactoryBean alertsSessionFactory(){ |
| 47 | + final AnnotationSessionFactoryBean alertsSessionFactory = new AnnotationSessionFactoryBean(); |
| 48 | + alertsSessionFactory.setDataSource( this.restDataSource() ); |
| 49 | + alertsSessionFactory.setPackagesToScan( new String[ ] { "org.rest" } ); |
| 50 | + alertsSessionFactory.setHibernateProperties( this.hibernateProperties() ); |
| 51 | + |
| 52 | + return alertsSessionFactory; |
| 53 | + } |
| 54 | + |
| 55 | + @Bean |
| 56 | + public HibernateTransactionManager transactionManager(){ |
| 57 | + final HibernateTransactionManager transactionManager = new HibernateTransactionManager(); |
| 58 | + transactionManager.setSessionFactory( this.alertsSessionFactory().getObject() ); |
| 59 | + |
| 60 | + return transactionManager; |
| 61 | + } |
| 62 | + |
| 63 | + final Properties hibernateProperties(){ |
| 64 | + return new Properties(){ |
| 65 | + { |
| 66 | + this.put( "hibernate.dialect", PersistenceConfig.this.hibernateDialect ); |
| 67 | + this.put( "hibernate.show_sql", PersistenceConfig.this.hibernateShowSql ); |
| 68 | + this.put( "hibernate.hbm2ddl.auto", PersistenceConfig.this.hibernateHbm2ddlAuto ); |
| 69 | + |
| 70 | + // in progresses still |
| 71 | + this.put( "hibernate.transaction.factory_class", "org.springframework.orm.hibernate3.SpringTransactionFactory" ); // SpringTransactionFactory.class.getSimpleName() |
| 72 | + this.put( "hibernate.current_session_context_class", "thread" ); |
| 73 | + } |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments