Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single DAO & generic CRUD methods (JPA/Hibernate + Spring)

Following my previous question, DAO and Service layers (JPA/Hibernate + Spring), I decided to use just a single DAO for my data layer (at least at the beginning) in an application using JPA/Hibernate, Spring and Wicket. The use of generic CRUD methods was proposed, but I'm not very sure how to implement this using JPA. Could you please give me an example or share a link regarding this?

like image 274
John Manak Avatar asked Oct 08 '10 07:10

John Manak


People also ask

Can one person own a DAO?

While one person or entity must form the DAO, they do not retain sole control over the DAO.

What a DAO means?

A DAO is a decentralized autonomous organization, a type of bottom-up entity structure with no central authority. Members of a DAO own tokens of the DAO, and members can vote on initiatives for the entity. Smart contracts are implemented for the DAO, and the code governing the DAO's operations in publicly disclosed.

What is the most successful DAO?

Uniswap is one of the biggest and most popular DAOs and operates as a cryptocurrency exchange built on the Ethereum blockchain. Anyone can become a member by holding the UNI token, which gives voting rights on the way the organization is run and administered.

What is an example of DAO?

Examples of operational DAOs include DASH, a cryptocurrency managed by its users, MakerDAO, a software that maintains a stablecoin, and Augur, a prediction market platform. Other use cases include incentivizing users to operate social media platforms, such as Steemit, or shared virtual worlds, such as Decentraland.


1 Answers

Here is an 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 an 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);     } } 
like image 136
Pascal Thivent Avatar answered Sep 24 '22 10:09

Pascal Thivent