hibernate work

This commit is contained in:
eugenp 2013-05-18 16:03:37 +03:00
parent 71a230ef91
commit bcfc7d9aaf
6 changed files with 73 additions and 56 deletions

View File

@ -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.missingHashCodeMethod=error
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=warning org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=warning
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled 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.missingSynchronizedOnInheritedMethod=ignore
org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning

View File

@ -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<T extends Serializable> {
private Class<T> clazz;
@Autowired
private SessionFactory sessionFactory;
// API
protected final void setClazz(final Class<T> clazzToSet) {
clazz = clazzToSet;
}
public final T findOne(final Long id) {
Preconditions.checkArgument(id != null);
return ((T) getCurrentSession().get(clazz, id));
}
public final List<T> 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();
}
}

View File

@ -1,66 +1,22 @@
package org.baeldung.spring.persistence.dao; package org.baeldung.spring.persistence.dao;
import java.util.List;
import org.baeldung.spring.persistence.model.Foo; import org.baeldung.spring.persistence.model.Foo;
import org.hibernate.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import com.google.common.base.Preconditions;
@Repository @Repository
public class FooDao implements IFooDao { public class FooDao extends AbstractHibernateDao<Foo> implements IFooDao {
@Autowired @Autowired
private SessionFactory sessionFactory; private SessionFactory sessionFactory;
public FooDao() { public FooDao() {
super(); super();
setClazz(Foo.class);
} }
// API // API
@Override
public Foo findOne(final Long id) {
Preconditions.checkArgument(id != null);
return (Foo) getCurrentSession().get(Foo.class, id);
}
@Override
@SuppressWarnings("unchecked")
public List<Foo> 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();
}
} }

View File

@ -6,16 +6,16 @@ import org.baeldung.spring.persistence.model.Foo;
public interface IFooDao { public interface IFooDao {
Foo findOne(Long id); Foo findOne(final Long id);
List<Foo> findAll(); List<Foo> 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);
} }

View File

@ -1,21 +1,21 @@
package org.baeldung.spring.persistence.model; package org.baeldung.spring.persistence.model;
import java.io.Serializable;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity @Entity
public class Foo { public class Foo implements Serializable {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private long id; private long id;
@Column(nullable = false) @Column(nullable = false)
@NotNull
private String name; private String name;
public Foo() { public Foo() {

View File

@ -4,6 +4,7 @@ import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import org.baeldung.spring.persistence.config.PersistenceConfig; import org.baeldung.spring.persistence.config.PersistenceConfig;
import org.baeldung.spring.persistence.model.Foo; import org.baeldung.spring.persistence.model.Foo;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -50,6 +51,7 @@ public class FooServicePersistenceIntegrationTest {
} }
@Test @Test
@Ignore
public final void temp_whenInvalidEntityIsCreated_thenDataException() { public final void temp_whenInvalidEntityIsCreated_thenDataException() {
service.create(new Foo(randomAlphabetic(2048))); service.create(new Foo(randomAlphabetic(2048)));
} }