persistence work
This commit is contained in:
parent
dfd157bb9d
commit
e8ffb362e6
|
@ -24,12 +24,18 @@
|
||||||
|
|
||||||
<!-- persistence -->
|
<!-- persistence -->
|
||||||
|
|
||||||
|
<!-- <dependency> -->
|
||||||
|
<!-- <groupId>org.hibernate</groupId> -->
|
||||||
|
<!-- <artifactId>hibernate-entitymanager</artifactId> -->
|
||||||
|
<!-- <version>4.2.1.Final</version> -->
|
||||||
|
<!-- </dependency> -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hibernate</groupId>
|
<groupId>org.hibernate</groupId>
|
||||||
<artifactId>hibernate-entitymanager</artifactId>
|
<artifactId>hibernate-core</artifactId>
|
||||||
<version>4.2.1.Final</version>
|
<version>4.2.1.Final</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package org.baeldung.spring.persistence.dao;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class FooDao {
|
||||||
|
|
||||||
|
public FooDao() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package org.baeldung.spring.persistence.model;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Foo {
|
||||||
|
//
|
||||||
|
}
|
|
@ -1,14 +0,0 @@
|
||||||
package org.baeldung.spring.web.config;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class PersistenceConfig {
|
|
||||||
|
|
||||||
public PersistenceConfig() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
// API
|
|
||||||
|
|
||||||
}
|
|
|
@ -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
|
|
@ -1,42 +1,40 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||||
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
xsi:schemaLocation="
|
||||||
xsi:schemaLocation="
|
|
||||||
http://java.sun.com/xml/ns/javaee
|
http://java.sun.com/xml/ns/javaee
|
||||||
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 MVC Application</display-name>
|
<display-name>Spring MVC Application</display-name>
|
||||||
|
|
||||||
<!-- Spring root -->
|
<!-- Spring root -->
|
||||||
<context-param>
|
<context-param>
|
||||||
<param-name>contextClass</param-name>
|
<param-name>contextClass</param-name>
|
||||||
<param-value>
|
<param-value>
|
||||||
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
|
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
|
||||||
</param-value>
|
</param-value>
|
||||||
</context-param>
|
</context-param>
|
||||||
<context-param>
|
<context-param>
|
||||||
<param-name>contextConfigLocation</param-name>
|
<param-name>contextConfigLocation</param-name>
|
||||||
<param-value>org.baeldung.spring.web.config</param-value>
|
<param-value>org.baeldung.spring.persistence.config</param-value>
|
||||||
</context-param>
|
</context-param>
|
||||||
|
|
||||||
<listener>
|
<listener>
|
||||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||||
</listener>
|
</listener>
|
||||||
|
|
||||||
<!-- Spring child -->
|
<!-- Spring child -->
|
||||||
<servlet>
|
<servlet>
|
||||||
<servlet-name>mvc</servlet-name>
|
<servlet-name>mvc</servlet-name>
|
||||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||||
<load-on-startup>1</load-on-startup>
|
<load-on-startup>1</load-on-startup>
|
||||||
</servlet>
|
</servlet>
|
||||||
<servlet-mapping>
|
<servlet-mapping>
|
||||||
<servlet-name>mvc</servlet-name>
|
<servlet-name>mvc</servlet-name>
|
||||||
<url-pattern>/</url-pattern>
|
<url-pattern>/</url-pattern>
|
||||||
</servlet-mapping>
|
</servlet-mapping>
|
||||||
|
|
||||||
<welcome-file-list>
|
<welcome-file-list>
|
||||||
<welcome-file>index.html</welcome-file>
|
<welcome-file>index.html</welcome-file>
|
||||||
</welcome-file-list>
|
</welcome-file-list>
|
||||||
|
|
||||||
</web-app>
|
</web-app>
|
Loading…
Reference in New Issue