diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/IFooDao.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/IFooDao.java new file mode 100644 index 0000000000..fc3928d8a6 --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/IFooDao.java @@ -0,0 +1,8 @@ +package org.baeldung.persistence.dao; + +import org.baeldung.persistence.dao.common.IOperations; +import org.baeldung.persistence.model.Foo; + +public interface IFooDao extends IOperations { + // +} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/common/AbstractHibernateDao.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/common/AbstractHibernateDao.java new file mode 100644 index 0000000000..65e57afcb3 --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/common/AbstractHibernateDao.java @@ -0,0 +1,65 @@ +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 implements IOperations { + private Class clazz; + + @Autowired + private SessionFactory sessionFactory; + + // API + + protected final void setClazz(final Class clazzToSet) { + clazz = Preconditions.checkNotNull(clazzToSet); + } + + @Override + public final T findOne(final long id) { + return (T) getCurrentSession().get(clazz, id); + } + + @Override + public final List 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(); + } + +} \ No newline at end of file diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/common/GenericHibernateDao.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/common/GenericHibernateDao.java new file mode 100644 index 0000000000..47ed130878 --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/common/GenericHibernateDao.java @@ -0,0 +1,13 @@ +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 extends AbstractHibernateDao implements IGenericDao { + // +} \ No newline at end of file diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/common/IGenericDao.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/common/IGenericDao.java new file mode 100644 index 0000000000..b7876deea4 --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/common/IGenericDao.java @@ -0,0 +1,7 @@ +package org.baeldung.persistence.dao.common; + +import java.io.Serializable; + +public interface IGenericDao extends IOperations { + // +} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/common/IOperations.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/common/IOperations.java new file mode 100644 index 0000000000..1c84b06c85 --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/common/IOperations.java @@ -0,0 +1,20 @@ +package org.baeldung.persistence.dao.common; + +import java.io.Serializable; +import java.util.List; + +public interface IOperations { + + T findOne(final long id); + + List findAll(); + + void create(final T entity); + + T update(final T entity); + + void delete(final T entity); + + void deleteById(final long entityId); + +} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/Foo.java new file mode 100644 index 0000000000..8e1dee33e8 --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/Foo.java @@ -0,0 +1,83 @@ +package org.baeldung.persistence.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Foo implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + @Column(nullable = false) + private String name; + + public Foo() { + super(); + } + + public Foo(final String name) { + super(); + + this.name = name; + } + + // API + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + // + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final Foo other = (Foo) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Foo [name=").append(name).append("]"); + return builder.toString(); + } + +} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/FooService.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/FooService.java deleted file mode 100644 index 02db7a733a..0000000000 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/FooService.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.baeldung.persistence.service; - -import org.baeldung.web.dto.Foo; -import org.springframework.stereotype.Service; - -@Service -public class FooService { - - public FooService() { - super(); - } - - // API - - public Foo getById(final Long id) { - return null; - } - - public Long create(final Foo resource) { - return null; - } - -} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/IFooService.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/IFooService.java new file mode 100644 index 0000000000..8774320450 --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/IFooService.java @@ -0,0 +1,8 @@ +package org.baeldung.persistence.service; + +import org.baeldung.persistence.dao.common.IOperations; +import org.baeldung.persistence.model.Foo; + +public interface IFooService extends IOperations { + // +} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/common/AbstractService.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/common/AbstractService.java new file mode 100644 index 0000000000..3b32bc3ebb --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/common/AbstractService.java @@ -0,0 +1,44 @@ +package org.baeldung.persistence.service.common; + +import java.io.Serializable; +import java.util.List; + +import org.baeldung.persistence.dao.common.IOperations; +import org.springframework.transaction.annotation.Transactional; + +@Transactional +public abstract class AbstractService implements IOperations { + + @Override + public T findOne(final long id) { + return getDao().findOne(id); + } + + @Override + public List 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 getDao(); + +} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/FooService.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/FooService.java new file mode 100644 index 0000000000..af77bef981 --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/FooService.java @@ -0,0 +1,30 @@ +package org.baeldung.persistence.service.impl; + +import org.baeldung.persistence.dao.IFooDao; +import org.baeldung.persistence.dao.common.IOperations; +import org.baeldung.persistence.model.Foo; +import org.baeldung.persistence.service.IFooService; +import org.baeldung.persistence.service.common.AbstractService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class FooService extends AbstractService implements IFooService { + + @Autowired + private IFooDao dao; + + public FooService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + +} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java index daa797ee36..b20eb3005e 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/FooController.java @@ -5,8 +5,8 @@ import java.net.URI; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.baeldung.persistence.service.FooService; -import org.baeldung.web.dto.Foo; +import org.baeldung.persistence.model.Foo; +import org.baeldung.persistence.service.impl.FooService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.http.HttpStatus; diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/dto/Foo.java b/spring-security-rest-full/src/main/java/org/baeldung/web/dto/Foo.java deleted file mode 100644 index 352045989d..0000000000 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/dto/Foo.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.baeldung.web.dto; - -import java.io.Serializable; - -public class Foo implements Serializable { - - public Foo() { - super(); - } - -}