java - Single DAO & generic CRUD methods (JPA/Hibernate + Spring) -
following previous question, dao , service layers (jpa/hibernate + spring), decided use single dao data layer (at least @ beginning) in application using jpa/hibernate, spring , wicket. use of generic crud methods proposed, i'm not sure how implement using jpa. please give me example or share link regarding this?
here example interface:
public interface genericdao<t, pk extends serializable> { t create(t t); t read(pk id); t update(t t); void delete(t t); }
and implementation:
public class genericdaojpaimpl<t, pk extends serializable> implements genericdao<t, pk> { protected class<t> entityclass; @persistencecontext protected entitymanager entitymanager; public genericdaojpaimpl() { parameterizedtype genericsuperclass = (parameterizedtype) getclass() .getgenericsuperclass(); this.entityclass = (class<t>) genericsuperclass .getactualtypearguments()[0]; } @override public t create(t t) { this.entitymanager.persist(t); return t; } @override public t read(pk id) { return this.entitymanager.find(entityclass, id); } @override public t update(t t) { return this.entitymanager.merge(t); } @override public void delete(t t) { t = this.entitymanager.merge(t); this.entitymanager.remove(t); } }
Comments
Post a Comment