persistence work

This commit is contained in:
Eugen 2013-06-03 18:35:13 +03:00
parent 6f8e29db09
commit 0fd0f22970
12 changed files with 39 additions and 101 deletions

View File

@ -80,6 +80,11 @@
<artifactId>spring-orm</artifactId> <artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version> <version>${org.springframework.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring-data-jpa.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.hibernate</groupId> <groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId> <artifactId>hibernate-core</artifactId>
@ -210,6 +215,7 @@
<!-- persistence --> <!-- persistence -->
<hibernate.version>4.2.2.Final</hibernate.version> <hibernate.version>4.2.2.Final</hibernate.version>
<mysql-connector-java.version>5.1.25</mysql-connector-java.version> <mysql-connector-java.version>5.1.25</mysql-connector-java.version>
<spring-data-jpa.version>1.3.2.RELEASE</spring-data-jpa.version>
<!-- logging --> <!-- logging -->
<org.slf4j.version>1.7.5</org.slf4j.version> <org.slf4j.version>1.7.5</org.slf4j.version>

View File

@ -1,4 +1,4 @@
package org.baeldung.persistence.dao.common; package org.baeldung.persistence;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
@ -9,7 +9,7 @@ public interface IOperations<T extends Serializable> {
List<T> findAll(); List<T> findAll();
void create(final T entity); T create(final T entity);
T update(final T entity); T update(final T entity);

View File

@ -1,8 +1,9 @@
package org.baeldung.persistence.dao; package org.baeldung.persistence.dao;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.model.Foo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface IFooDao extends IOperations<Foo> { public interface IFooDao extends JpaRepository<Foo, Long>, JpaSpecificationExecutor<Foo> {
// //
} }

View File

@ -1,65 +0,0 @@
package org.baeldung.persistence.dao.common;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.common.base.Preconditions;
@SuppressWarnings("unchecked")
public abstract class AbstractHibernateDao<T extends Serializable> implements IOperations<T> {
private Class<T> clazz;
@Autowired
private SessionFactory sessionFactory;
// API
protected final void setClazz(final Class<T> clazzToSet) {
clazz = Preconditions.checkNotNull(clazzToSet);
}
@Override
public final T findOne(final long id) {
return (T) getCurrentSession().get(clazz, id);
}
@Override
public final List<T> findAll() {
return getCurrentSession().createQuery("from " + clazz.getName()).list();
}
@Override
public final void create(final T entity) {
Preconditions.checkNotNull(entity);
// getCurrentSession().persist(entity);
getCurrentSession().saveOrUpdate(entity);
}
@Override
public final T update(final T entity) {
Preconditions.checkNotNull(entity);
return (T) getCurrentSession().merge(entity);
}
@Override
public final void delete(final T entity) {
Preconditions.checkNotNull(entity);
getCurrentSession().delete(entity);
}
@Override
public final void deleteById(final long entityId) {
final T entity = findOne(entityId);
Preconditions.checkState(entity != null);
delete(entity);
}
protected final Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
}

View File

@ -1,13 +0,0 @@
package org.baeldung.persistence.dao.common;
import java.io.Serializable;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
@Repository
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class GenericHibernateDao<T extends Serializable> extends AbstractHibernateDao<T> implements IGenericDao<T> {
//
}

View File

@ -1,7 +0,0 @@
package org.baeldung.persistence.dao.common;
import java.io.Serializable;
public interface IGenericDao<T extends Serializable> extends IOperations<T> {
//
}

View File

@ -1,6 +1,6 @@
package org.baeldung.persistence.service; package org.baeldung.persistence.service;
import org.baeldung.persistence.dao.common.IOperations; import org.baeldung.persistence.IOperations;
import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.model.Foo;
public interface IFooService extends IOperations<Foo> { public interface IFooService extends IOperations<Foo> {

View File

@ -3,30 +3,35 @@ package org.baeldung.persistence.service.common;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
import org.baeldung.persistence.dao.common.IOperations; import org.baeldung.persistence.IOperations;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
@Transactional @Transactional
public abstract class AbstractService<T extends Serializable> implements IOperations<T> { public abstract class AbstractService<T extends Serializable> implements IOperations<T> {
@Override @Override
@Transactional(readOnly = true)
public T findOne(final long id) { public T findOne(final long id) {
return getDao().findOne(id); return getDao().findOne(id);
} }
@Override @Override
@Transactional(readOnly = true)
public List<T> findAll() { public List<T> findAll() {
return getDao().findAll(); return Lists.newArrayList(getDao().findAll());
} }
@Override @Override
public void create(final T entity) { public T create(final T entity) {
getDao().create(entity); return getDao().save(entity);
} }
@Override @Override
public T update(final T entity) { public T update(final T entity) {
return getDao().update(entity); return getDao().save(entity);
} }
@Override @Override
@ -36,9 +41,9 @@ public abstract class AbstractService<T extends Serializable> implements IOperat
@Override @Override
public void deleteById(final long entityId) { public void deleteById(final long entityId) {
getDao().deleteById(entityId); getDao().delete(entityId);
} }
protected abstract IOperations<T> getDao(); protected abstract PagingAndSortingRepository<T, Long> getDao();
} }

View File

@ -1,11 +1,11 @@
package org.baeldung.persistence.service.impl; package org.baeldung.persistence.service.impl;
import org.baeldung.persistence.dao.IFooDao; import org.baeldung.persistence.dao.IFooDao;
import org.baeldung.persistence.dao.common.IOperations;
import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IFooService; import org.baeldung.persistence.service.IFooService;
import org.baeldung.persistence.service.common.AbstractService; import org.baeldung.persistence.service.common.AbstractService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -23,7 +23,7 @@ public class FooService extends AbstractService<Foo> implements IFooService {
// API // API
@Override @Override
protected IOperations<Foo> getDao() { protected PagingAndSortingRepository<Foo, Long> getDao() {
return dao; return dao;
} }

View File

@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource; 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;
@ -22,6 +23,7 @@ import com.google.common.base.Preconditions;
@EnableTransactionManagement @EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" }) @PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.baeldung.persistence" }) @ComponentScan({ "org.baeldung.persistence" })
@ImportResource("classpath*:springDataPersistenceConfig.xml")
public class PersistenceConfig { public class PersistenceConfig {
@Autowired @Autowired

View File

@ -47,7 +47,7 @@ public class FooController {
@RequestMapping(value = "admin/foo/{id}", method = RequestMethod.GET) @RequestMapping(value = "admin/foo/{id}", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public Foo get(@PathVariable("id") final Long id, final HttpServletRequest request, final HttpServletResponse response) { public Foo get(@PathVariable("id") final Long id, final HttpServletRequest request, final HttpServletResponse response) {
final Foo resourceById = Preconditions.checkNotNull(service.getById(id)); final Foo resourceById = Preconditions.checkNotNull(service.findOne(id));
eventPublisher.publishEvent(new SingleResourceRetrieved(this, request, response)); eventPublisher.publishEvent(new SingleResourceRetrieved(this, request, response));
return resourceById; return resourceById;
@ -57,7 +57,7 @@ public class FooController {
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody final Foo resource, final HttpServletRequest request, final HttpServletResponse response) { public void create(@RequestBody final Foo resource, final HttpServletRequest request, final HttpServletResponse response) {
Preconditions.checkNotNull(resource); Preconditions.checkNotNull(resource);
final Long idOfCreatedResource = service.create(resource); final Long idOfCreatedResource = service.create(resource).getId();
eventPublisher.publishEvent(new ResourceCreated(this, request, response, idOfCreatedResource)); eventPublisher.publishEvent(new ResourceCreated(this, request, response, idOfCreatedResource));
} }

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jpa:repositories base-package="org.baeldung.persistence.dao" />
</beans>