Created
December 7, 2011 20:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
} | |
} |
This gist is also used in:
http://www.baeldung.com/2011/12/08/simplifying-the-data-access-layer-with-spring-and-java-generics/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is used in:
http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/