Skip to content

Instantly share code, notes, and snippets.

@eugenp
Created December 7, 2011 20:02
Show Gist options
  • Save eugenp/1444373 to your computer and use it in GitHub Desktop.
Save eugenp/1444373 to your computer and use it in GitHub Desktop.
Simplyfing the DAO layer with Spring and Java Generics - the Abstract JPA DAO
public abstract class AbstractJpaDAO< T extends Serializable > {
private Class< T > clazz;
@PersistenceContext
EntityManager entityManager;
public void setClazz( final Class< T > clazzToSet ){
this.clazz = clazzToSet;
}
public T findOne( final Long id ){
return entityManager.find( clazz, id );
}
public List< T > findAll(){
return entityManager.createQuery( "from " + clazz.getName() )
.getResultList();
}
public void save( final T entity ){
entityManager.persist( entity );
}
public void update( final T entity ){
entityManager.merge( entity );
}
public void delete( final T entity ){
entityManager.remove( entity );
}
public void deleteById( final Long entityId ){
final T entity = getById( entityId );
delete( entity );
}
}
@eugenp
Copy link
Author

eugenp commented Dec 14, 2011

@eugenp
Copy link
Author

eugenp commented Dec 14, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment