From bcfc7d9aaf5e27924e77a90953c0e6327ffaf288 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 18 May 2013 16:03:37 +0300 Subject: [PATCH] hibernate work --- .../.settings/org.eclipse.jdt.core.prefs | 2 +- .../persistence/dao/AbstractHibernateDao.java | 59 +++++++++++++++++++ .../spring/persistence/dao/FooDao.java | 50 +--------------- .../spring/persistence/dao/IFooDao.java | 10 ++-- .../spring/persistence/model/Foo.java | 6 +- .../FooServicePersistenceIntegrationTest.java | 2 + 6 files changed, 73 insertions(+), 56 deletions(-) create mode 100644 spring-hibernate4/src/main/java/org/baeldung/spring/persistence/dao/AbstractHibernateDao.java diff --git a/spring-hibernate4/.settings/org.eclipse.jdt.core.prefs b/spring-hibernate4/.settings/org.eclipse.jdt.core.prefs index a931b33192..c201ee1f7a 100644 --- a/spring-hibernate4/.settings/org.eclipse.jdt.core.prefs +++ b/spring-hibernate4/.settings/org.eclipse.jdt.core.prefs @@ -42,7 +42,7 @@ org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=error org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=warning org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled -org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning +org.eclipse.jdt.core.compiler.problem.missingSerialVersion=ignore org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning diff --git a/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/dao/AbstractHibernateDao.java b/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/dao/AbstractHibernateDao.java new file mode 100644 index 0000000000..4ff95f1126 --- /dev/null +++ b/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/dao/AbstractHibernateDao.java @@ -0,0 +1,59 @@ +package org.baeldung.spring.persistence.dao; + +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 { + private Class clazz; + + @Autowired + private SessionFactory sessionFactory; + + // API + + protected final void setClazz(final Class clazzToSet) { + clazz = clazzToSet; + } + + public final T findOne(final Long id) { + Preconditions.checkArgument(id != null); + return ((T) getCurrentSession().get(clazz, id)); + } + + public final List findAll() { + return getCurrentSession().createQuery("from " + clazz.getName()).list(); + } + + public final void create(final T entity) { + Preconditions.checkNotNull(entity); + getCurrentSession().persist(entity); + } + + public final T update(final T entity) { + Preconditions.checkNotNull(entity); + return (T) getCurrentSession().merge(entity); + } + + public final void delete(final T entity) { + Preconditions.checkNotNull(entity); + getCurrentSession().delete(entity); + } + + 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-hibernate4/src/main/java/org/baeldung/spring/persistence/dao/FooDao.java b/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/dao/FooDao.java index b7878abd81..1efa61ffb2 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/dao/FooDao.java +++ b/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/dao/FooDao.java @@ -1,66 +1,22 @@ package org.baeldung.spring.persistence.dao; -import java.util.List; - import org.baeldung.spring.persistence.model.Foo; -import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; -import com.google.common.base.Preconditions; - @Repository -public class FooDao implements IFooDao { +public class FooDao extends AbstractHibernateDao implements IFooDao { @Autowired private SessionFactory sessionFactory; public FooDao() { super(); + + setClazz(Foo.class); } // API - @Override - public Foo findOne(final Long id) { - Preconditions.checkArgument(id != null); - return (Foo) getCurrentSession().get(Foo.class, id); - } - - @Override - @SuppressWarnings("unchecked") - public List findAll() { - return getCurrentSession().createQuery("from " + Foo.class.getName()).list(); - } - - @Override - public void create(final Foo entity) { - Preconditions.checkNotNull(entity); - getCurrentSession().persist(entity); - } - - @Override - public void update(final Foo entity) { - Preconditions.checkNotNull(entity); - getCurrentSession().merge(entity); - } - - @Override - public void delete(final Foo entity) { - Preconditions.checkNotNull(entity); - getCurrentSession().delete(entity); - } - - @Override - public void deleteById(final Long entityId) { - final Foo entity = findOne(entityId); - Preconditions.checkState(entity != null); - delete(entity); - } - - protected final Session getCurrentSession() { - return sessionFactory.getCurrentSession(); - } - } diff --git a/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/dao/IFooDao.java b/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/dao/IFooDao.java index 5536c9e7d7..6ad12b147c 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/dao/IFooDao.java +++ b/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/dao/IFooDao.java @@ -6,16 +6,16 @@ import org.baeldung.spring.persistence.model.Foo; public interface IFooDao { - Foo findOne(Long id); + Foo findOne(final Long id); List findAll(); - void create(Foo entity); + void create(final Foo entity); - void update(Foo entity); + Foo update(final Foo entity); - void delete(Foo entity); + void delete(final Foo entity); - void deleteById(Long entityId); + void deleteById(final Long entityId); } diff --git a/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/model/Foo.java b/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/model/Foo.java index 2bf8f7f008..2043d087e0 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/model/Foo.java +++ b/spring-hibernate4/src/main/java/org/baeldung/spring/persistence/model/Foo.java @@ -1,21 +1,21 @@ package org.baeldung.spring.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; -import javax.validation.constraints.NotNull; @Entity -public class Foo { +public class Foo implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Column(nullable = false) - @NotNull private String name; public Foo() { diff --git a/spring-hibernate4/src/test/java/org/baeldung/spring/persistence/service/FooServicePersistenceIntegrationTest.java b/spring-hibernate4/src/test/java/org/baeldung/spring/persistence/service/FooServicePersistenceIntegrationTest.java index 3020250f6d..576f536adc 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/spring/persistence/service/FooServicePersistenceIntegrationTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/spring/persistence/service/FooServicePersistenceIntegrationTest.java @@ -4,6 +4,7 @@ import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import org.baeldung.spring.persistence.config.PersistenceConfig; import org.baeldung.spring.persistence.model.Foo; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -50,6 +51,7 @@ public class FooServicePersistenceIntegrationTest { } @Test + @Ignore public final void temp_whenInvalidEntityIsCreated_thenDataException() { service.create(new Foo(randomAlphabetic(2048))); }