DAO
This commit is contained in:
31
src/main/java/DAO/TousDAOImpl.java
Normal file
31
src/main/java/DAO/TousDAOImpl.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package DAO;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityTransaction;
|
||||
import jakarta.persistence.PersistenceException;
|
||||
import jpa.EntityManagerHelper;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public abstract class TousDAOImpl<T, Id extends Serializable> implements Tous<T, Id> {
|
||||
protected EntityManager em;
|
||||
private Class<T> entityClass;
|
||||
|
||||
public TousDAOImpl() {
|
||||
this.em = EntityManagerHelper.getEntityManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T save(T t) {
|
||||
EntityTransaction tx = em.getTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
em.persist(t);
|
||||
tx.commit();
|
||||
return t;
|
||||
} catch (PersistenceException e) {
|
||||
throw new PersistenceException(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user