diff --git a/spring-hibernate-dao/pom.xml b/spring-hibernate-dao/pom.xml index 3f5af70990..ff6b794580 100644 --- a/spring-hibernate-dao/pom.xml +++ b/spring-hibernate-dao/pom.xml @@ -24,12 +24,18 @@ + + + + + org.hibernate - hibernate-entitymanager + hibernate-core 4.2.1.Final + diff --git a/spring-hibernate-dao/src/main/java/org/baeldung/spring/persistence/config/PersistenceConfig.java b/spring-hibernate-dao/src/main/java/org/baeldung/spring/persistence/config/PersistenceConfig.java new file mode 100644 index 0000000000..33833f364c --- /dev/null +++ b/spring-hibernate-dao/src/main/java/org/baeldung/spring/persistence/config/PersistenceConfig.java @@ -0,0 +1,73 @@ +package org.baeldung.spring.persistence.config; + +import java.util.Properties; + +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.core.env.Environment; +import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean; +import org.springframework.orm.hibernate4.HibernateTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@Configuration +@EnableTransactionManagement +@ComponentScan({ "org.baeldung.spring.persistence.dao", "org.baeldung.spring.persistence.service" }) +public class PersistenceConfig { + + @Autowired + private Environment env; + + public PersistenceConfig() { + super(); + } + + @Bean + public AnnotationSessionFactoryBean alertsSessionFactory() { + final AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean(); + sessionFactory.setDataSource(restDataSource()); + sessionFactory.setPackagesToScan(new String[] { "org.rest" }); + sessionFactory.setHibernateProperties(hibernateProperties()); + + return sessionFactory; + } + + @Bean + public DataSource restDataSource() { + final DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); + dataSource.setUrl(env.getProperty("jdbc.url")); + dataSource.setUsername(env.getProperty("jdbc.user")); + dataSource.setPassword(env.getProperty("jdbc.pass")); + + return dataSource; + } + + @Bean + public HibernateTransactionManager transactionManager() { + final HibernateTransactionManager txManager = new HibernateTransactionManager(); + txManager.setSessionFactory(alertsSessionFactory().getObject()); + + return txManager; + } + + @Bean + public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { + return new PersistenceExceptionTranslationPostProcessor(); + } + + final Properties hibernateProperties() { + return new Properties() { + { + setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); + // setProperty("hibernate.globally_quoted_identifiers", "true"); + // note: necessary in launchpad-storage, but causing problems here + } + }; + } +} \ No newline at end of file diff --git a/spring-hibernate-dao/src/main/java/org/baeldung/spring/persistence/dao/FooDao.java b/spring-hibernate-dao/src/main/java/org/baeldung/spring/persistence/dao/FooDao.java new file mode 100644 index 0000000000..a497d14dd4 --- /dev/null +++ b/spring-hibernate-dao/src/main/java/org/baeldung/spring/persistence/dao/FooDao.java @@ -0,0 +1,14 @@ +package org.baeldung.spring.persistence.dao; + +import org.springframework.stereotype.Repository; + +@Repository +public class FooDao { + + public FooDao() { + super(); + } + + // API + +} diff --git a/spring-hibernate-dao/src/main/java/org/baeldung/spring/persistence/model/Foo.java b/spring-hibernate-dao/src/main/java/org/baeldung/spring/persistence/model/Foo.java new file mode 100644 index 0000000000..01a3693325 --- /dev/null +++ b/spring-hibernate-dao/src/main/java/org/baeldung/spring/persistence/model/Foo.java @@ -0,0 +1,8 @@ +package org.baeldung.spring.persistence.model; + +import javax.persistence.Entity; + +@Entity +public class Foo { + // +} diff --git a/spring-hibernate-dao/src/main/java/org/baeldung/spring/web/config/PersistenceConfig.java b/spring-hibernate-dao/src/main/java/org/baeldung/spring/web/config/PersistenceConfig.java deleted file mode 100644 index b3348756e7..0000000000 --- a/spring-hibernate-dao/src/main/java/org/baeldung/spring/web/config/PersistenceConfig.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.baeldung.spring.web.config; - -import org.springframework.context.annotation.Configuration; - -@Configuration -public class PersistenceConfig { - - public PersistenceConfig() { - super(); - } - - // API - -} \ No newline at end of file diff --git a/spring-hibernate-dao/src/main/resources/persistence-mysql.properties b/spring-hibernate-dao/src/main/resources/persistence-mysql.properties new file mode 100644 index 0000000000..a1ff997616 --- /dev/null +++ b/spring-hibernate-dao/src/main/resources/persistence-mysql.properties @@ -0,0 +1,10 @@ +# jdbc.X +jdbc.driverClassName=com.mysql.jdbc.Driver +jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate_dao_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 diff --git a/spring-hibernate-dao/src/main/webapp/WEB-INF/web.xml b/spring-hibernate-dao/src/main/webapp/WEB-INF/web.xml index ba2bb591ed..7e3d3b3181 100644 --- a/spring-hibernate-dao/src/main/webapp/WEB-INF/web.xml +++ b/spring-hibernate-dao/src/main/webapp/WEB-INF/web.xml @@ -1,42 +1,40 @@ - + http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> - Spring MVC Application + Spring MVC Application - - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - org.baeldung.spring.web.config - + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + org.baeldung.spring.persistence.config + - - org.springframework.web.context.ContextLoaderListener - + + org.springframework.web.context.ContextLoaderListener + - - - mvc - org.springframework.web.servlet.DispatcherServlet - 1 - - - mvc - / - + + + mvc + org.springframework.web.servlet.DispatcherServlet + 1 + + + mvc + / + - - index.html - + + index.html + \ No newline at end of file