Skip to content

Instantly share code, notes, and snippets.

@jeremyheiler
Created August 29, 2011 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyheiler/1178776 to your computer and use it in GitHub Desktop.
Save jeremyheiler/1178776 to your computer and use it in GitHub Desktop.
@Entity
public class Foo{
@Id
public int id;
}
/**
* <p>The Repository is the interface for which the application interacts with the persistent context. This interface
* closely resembles the JPA EntityManager, as the primary implementation wraps it.</p>
*/
public interface Repository{
public boolean contains(Object entity);
public void detach(Object entity);
public <E> E find(Class<E> entityType, Object id);
public void persist(Object entity);
public void refresh(Object entity);
public void remove(Object entity);
public <E> E merge(E entity);
}
/**
* <p>A Repository implementation for JPA, which essentially wraps the JPA EntityManager. It is intended that you create
* an instance of this class whenever you need access to the persistent context.</p>
*/
public final class JpaRepository implements Repository{
private final EntityManager entityManager;
@Inject
public JpaRepository(EntityManager entityManager){
this.entityManager = entityManager;
}
/**
* Provides the EntityManager for code that needs it when working directly with JpaRepository.
* @return The EntityManager this class wraps.
*/
public EntityManager getEntityManager(){
return entityManager;
}
@Override
public boolean contains(Object entity){
return entityManager.contains(entity);
}
@Override
public void detach(Object entity){
entityManager.detach(entity);
}
@Override
public <E> E find(Class<E> entityType, Object id){
return entityManager.find(entityType, id);
}
@Override
public void persist(Object entity){
entityManager.persist(entity);
}
@Override
public void refresh(Object entity){
entityManager.refresh(entity);
}
@Override
public void remove(Object entity){
entityManager.remove(entity);
}
@Override
public <E> E merge(E entity){
return entityManager.merge(entity);
}
}
/**
* <p>This interface provides an API for interacting with the persistent context in terms of a specific entity. Unlike
* the Repository interface which can be used to interact with the entire perstent context, this interface restricts
* interaction to a specific entity. This is useful when you want to abstract away queries that deal with the entity.</p>
* <p>In general, you would create an interface that extends this one, and then write a class that implements that interface.</p>
* <code>
* public interface FooRepository extends EntityRepository<Integer,Foo>{
* public List<Foo> findFoosWithBar(Bar bar);
* }
*
* public class FooJpaRepository extends JpaEntityRepository<Integer,Foo> implements FooRepsitory{
*
* public FooJpaRepository(JpaRepository repository){
* super(repository, Foo.class);
* }
*
* @Override
* public List<Foo> findFoosWithBar(Bar bar){
* TypedQuery<Foo> query = repository.getEntityManager().createQuery(...);
* query.setParameter(...);
* return query.getResultList();
* }
* }
* </code>
*
* @param <I> The class that represents the id of the entity.
* @param <E> The entity class.
*/
public interface EntityRepository<I,E>{
public boolean contains(E entity);
public void detach(E entity);
public E find(I id);
public void persist(E entity);
public void refresh(E entity);
public void remove(E entity);
public E merge(E entity);
}
/**
* Extend this class if you intend on implementing EntityRepository with a JPA persistent context.
*
* This class requires all sub classes to have a constructor that sets an instance of JpaRepository and the entity's type.
*
* @param <I> The class that represents the id of the entity.
* @param <E> The entity class.
*/
public abstract class JpaEntityRepository<I,E> implements EntityRepository<I,E>{
protected final JpaRepository repository;
protected final Class<E> entityType;
protected JpaEntityRepository(JpaRepository repository, Class<E> entityType){
this.repository = repository;
this.entityType = entityType;
}
@Override
public boolean contains(E entity){
return repository.contains(entity);
}
@Override
public void detach(E entity){
repository.detach(entity);
}
@Override
public E find(I id){
return repository.find(entityType, id);
}
@Override
public void persist(E entity){
repository.persist(entity);
}
@Override
public void refresh(E entity){
repository.refresh(entity);
}
@Override
public void remove(E entity){
repository.remove(entity);
}
@Override
public E merge(E entity){
return repository.merge(entity);
}
}
public interface FooRepository extends EntityRepository<Integer,Foo>{
public List<Foo> loadManyFoos();
}
public class FooJpaRepository extends JpaEntityRepository<Integer,Foo> implements FooRepository{
@Inject
public FooJpaRepository(JpaRepository repository){
super(repository, Foo.class);
}
@Override
public List<Foo> loadManyFoos(){
//entityManager.createQuery(...);
return new ArrayList<Foo>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment