hibernate work
This commit is contained in:
parent
71a230ef91
commit
bcfc7d9aaf
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Foo> 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<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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,16 +6,16 @@ import org.baeldung.spring.persistence.model.Foo;
|
|||
|
||||
public interface IFooDao {
|
||||
|
||||
Foo findOne(Long id);
|
||||
Foo findOne(final Long id);
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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)));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue