dao work
This commit is contained in:
parent
5fc81e081e
commit
a746480326
@ -0,0 +1,46 @@
|
|||||||
|
package org.baeldung.persistence.dao;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
|
||||||
|
public abstract class AbstractJpaDAO<T extends Serializable> {
|
||||||
|
|
||||||
|
private Class<T> clazz;
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
|
public final void setClazz(final Class<T> clazzToSet) {
|
||||||
|
this.clazz = clazzToSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T findOne(final long id) {
|
||||||
|
return entityManager.find(clazz, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public List<T> findAll() {
|
||||||
|
return entityManager.createQuery("from " + clazz.getName()).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void create(final T entity) {
|
||||||
|
entityManager.persist(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T update(final T entity) {
|
||||||
|
return entityManager.merge(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(final T entity) {
|
||||||
|
entityManager.remove(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteById(final long entityId) {
|
||||||
|
final T entity = findOne(entityId);
|
||||||
|
delete(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,60 +1,17 @@
|
|||||||
package org.baeldung.persistence.dao;
|
package org.baeldung.persistence.dao;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.PersistenceContext;
|
|
||||||
|
|
||||||
import org.baeldung.persistence.model.Foo;
|
import org.baeldung.persistence.model.Foo;
|
||||||
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 AbstractJpaDAO<Foo> implements IFooDao {
|
||||||
|
|
||||||
@PersistenceContext
|
|
||||||
private EntityManager entityManager;
|
|
||||||
|
|
||||||
public FooDao() {
|
public FooDao() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
setClazz(Foo.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
// API
|
// API
|
||||||
|
|
||||||
@Override
|
|
||||||
public Foo findOne(final long id) {
|
|
||||||
return entityManager.find(Foo.class, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public List<Foo> findAll() {
|
|
||||||
return entityManager.createQuery("from " + Foo.class.getName()).getResultList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void create(final Foo entity) {
|
|
||||||
Preconditions.checkNotNull(entity);
|
|
||||||
entityManager.persist(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Foo update(final Foo entity) {
|
|
||||||
Preconditions.checkNotNull(entity);
|
|
||||||
return entityManager.merge(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void delete(final Foo entity) {
|
|
||||||
Preconditions.checkNotNull(entity);
|
|
||||||
entityManager.remove(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void deleteById(final long entityId) {
|
|
||||||
final Foo entity = findOne(entityId);
|
|
||||||
delete(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package org.baeldung.persistence.model;
|
package org.baeldung.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;
|
||||||
@ -7,7 +9,7 @@ import javax.persistence.GenerationType;
|
|||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Foo {
|
public class Foo implements Serializable {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user