further persistence work
This commit is contained in:
parent
f147b8b0e1
commit
3bbfdf175b
|
@ -50,6 +50,11 @@
|
|||
<version>${mysql-connector-java.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-dbcp</artifactId>
|
||||
<version>${tomcat-dbcp.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- validation -->
|
||||
|
||||
|
@ -188,8 +193,9 @@
|
|||
<jstl.version>1.2</jstl.version>
|
||||
|
||||
<!-- persistence -->
|
||||
<hibernate.version>4.2.3.Final</hibernate.version>
|
||||
<hibernate.version>4.2.4.Final</hibernate.version>
|
||||
<mysql-connector-java.version>5.1.26</mysql-connector-java.version>
|
||||
<tomcat-dbcp.version>7.0.42</tomcat-dbcp.version>
|
||||
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.5</org.slf4j.version>
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
package org.baeldung.ex.nosuchbeandefinitionexception.spring;
|
||||
|
||||
import org.baeldung.persistence.spring.PersistenceConfig;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.ex.nosuchbeandefinitionexception.cause1")
|
||||
@Import(PersistenceConfig.class)
|
||||
public class Cause1ContextWithJavaConfig {
|
||||
|
||||
public Cause1ContextWithJavaConfig() {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.dao.common;
|
||||
package org.baeldung.persistence.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
|
@ -0,0 +1,44 @@
|
|||
package org.baeldung.persistence.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.common.IOperations;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Transactional
|
||||
public abstract class AbstractService<T extends Serializable> implements IOperations<T> {
|
||||
|
||||
@Override
|
||||
public T findOne(final long id) {
|
||||
return getDao().findOne(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> findAll() {
|
||||
return getDao().findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(final T entity) {
|
||||
getDao().create(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T update(final T entity) {
|
||||
return getDao().update(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(final T entity) {
|
||||
getDao().delete(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(final long entityId) {
|
||||
getDao().deleteById(entityId);
|
||||
}
|
||||
|
||||
protected abstract IOperations<T> getDao();
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.dao.common;
|
||||
package org.baeldung.persistence.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.dao.common.IOperations;
|
||||
import org.baeldung.persistence.common.IOperations;
|
||||
import org.baeldung.persistence.model.Child;
|
||||
|
||||
public interface IChildDao extends IOperations<Child> {
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.dao.common.IOperations;
|
||||
import org.baeldung.persistence.common.IOperations;
|
||||
import org.baeldung.persistence.model.Parent;
|
||||
|
||||
public interface IParentDao extends IOperations<Parent> {
|
|
@ -1,7 +1,7 @@
|
|||
package org.baeldung.persistence.dao.impl;
|
||||
|
||||
import org.baeldung.persistence.common.AbstractHibernateDao;
|
||||
import org.baeldung.persistence.dao.IChildDao;
|
||||
import org.baeldung.persistence.dao.common.AbstractHibernateDao;
|
||||
import org.baeldung.persistence.model.Child;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -1,7 +1,7 @@
|
|||
package org.baeldung.persistence.dao.impl;
|
||||
|
||||
import org.baeldung.persistence.common.AbstractHibernateDao;
|
||||
import org.baeldung.persistence.dao.IParentDao;
|
||||
import org.baeldung.persistence.dao.common.AbstractHibernateDao;
|
||||
import org.baeldung.persistence.model.Parent;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.persistence.dao.common.IOperations;
|
||||
import org.baeldung.persistence.common.IOperations;
|
||||
import org.baeldung.persistence.model.Child;
|
||||
|
||||
public interface IChildService extends IOperations<Child> {
|
|
@ -1,6 +1,6 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.persistence.dao.common.IOperations;
|
||||
import org.baeldung.persistence.common.IOperations;
|
||||
import org.baeldung.persistence.model.Parent;
|
||||
|
||||
public interface IParentService extends IOperations<Parent> {
|
|
@ -0,0 +1,29 @@
|
|||
package org.baeldung.persistence.service.impl;
|
||||
|
||||
import org.baeldung.persistence.common.AbstractService;
|
||||
import org.baeldung.persistence.common.IOperations;
|
||||
import org.baeldung.persistence.dao.IChildDao;
|
||||
import org.baeldung.persistence.model.Child;
|
||||
import org.baeldung.persistence.service.IChildService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
@Service
|
||||
public class ChildService extends AbstractService<Child> implements IChildService {
|
||||
|
||||
@Autowired
|
||||
private IChildDao dao;
|
||||
|
||||
public ChildService() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
protected IOperations<Child> getDao() {
|
||||
return dao;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package org.baeldung.persistence.service.impl;
|
||||
|
||||
import org.baeldung.persistence.common.AbstractService;
|
||||
import org.baeldung.persistence.common.IOperations;
|
||||
import org.baeldung.persistence.dao.IParentDao;
|
||||
import org.baeldung.persistence.model.Parent;
|
||||
import org.baeldung.persistence.service.IParentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ParentService extends AbstractService<Parent> implements IParentService {
|
||||
|
||||
@Autowired
|
||||
private IParentDao dao;
|
||||
|
||||
public ParentService() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
protected IOperations<Parent> getDao() {
|
||||
return dao;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
# jdbc.X
|
||||
jdbc.driverClassName=com.mysql.jdbc.Driver
|
||||
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate4_exceptions?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
|
|
@ -0,0 +1,79 @@
|
|||
package org.baeldung.persistence.spring;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
|
||||
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.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
||||
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-mysql.properties" })
|
||||
@ComponentScan({ "org.baeldung.persistence" })
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public PersistenceConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalSessionFactoryBean sessionFactory() {
|
||||
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
||||
sessionFactory.setDataSource(restDataSource());
|
||||
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||
|
||||
return sessionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource restDataSource() {
|
||||
final BasicDataSource dataSource = new BasicDataSource();
|
||||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HibernateTransactionManager transactionManager() {
|
||||
final HibernateTransactionManager txManager = new HibernateTransactionManager();
|
||||
txManager.setSessionFactory(sessionFactory().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.dialect", env.getProperty("hibernate.dialect"));
|
||||
|
||||
// setProperty("hibernate.globally_quoted_identifiers", "true");
|
||||
// note: necessary in launchpad-storage, but causing problems here
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -171,9 +171,9 @@
|
|||
<javassist.version>3.18.0-GA</javassist.version>
|
||||
|
||||
<!-- persistence -->
|
||||
<hibernate.version>4.2.3.Final</hibernate.version>
|
||||
<hibernate.version>4.2.4.Final</hibernate.version>
|
||||
<mysql-connector-java.version>5.1.26</mysql-connector-java.version>
|
||||
<tomcat-dbcp.version>7.0.41</tomcat-dbcp.version>
|
||||
<tomcat-dbcp.version>7.0.42</tomcat-dbcp.version>
|
||||
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.5</org.slf4j.version>
|
||||
|
|
Loading…
Reference in New Issue