DAO commit

This commit is contained in:
tuanvu
2025-09-19 00:27:06 +02:00
parent d47024314a
commit f9351c9f75
19 changed files with 181 additions and 98 deletions

View File

@@ -1,4 +0,0 @@
package DAO;
public class ChoixDAO {
}

View File

@@ -0,0 +1,13 @@
package DAO;
import java.io.Serializable;
import java.util.List;
public interface Generic<K, T extends Serializable> {
void create(final T entity);
T findById(final K id);
T update(final T entity);
void delete(final T entity);
void deleteById(final K id);
List<T> findAll();
}

View File

@@ -0,0 +1,60 @@
package DAO;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.PersistenceException;
import jpa.EntityManagerHelper;
import java.io.Serializable;
import java.util.List;
public abstract class GenericDAOImpl<K, T extends Serializable> implements Generic<K, T> {
protected EntityManager em;
private Class<T> entityClass;
public GenericDAOImpl() {
this.em = EntityManagerHelper.getEntityManager();
}
//Pour connaitre qu'on travaille avec quelle classe
public void setClass(Class<T> class2){
this.entityClass= class2;
}
public List<T> findAll() {
return em.createQuery("select e from " + entityClass.getName() + " as e", entityClass).getResultList();
}
//CRUD
public void create(T entity){
EntityTransaction t = em.getTransaction();
t.begin();
em.persist(entity);
t.commit();
}
public T findById(K id) {
return em.find(entityClass, id);
}
public T update(T entity){
EntityTransaction t = em.getTransaction();
t.begin();
T T_sync = em.merge(entity);
t.commit();
return T_sync;
}
public void delete(T entity){
EntityTransaction t = em.getTransaction();
t.begin();
em.remove(entity);
t.commit();
}
public void deleteById(K id) {
T T_to_be_deleted = em.find(entityClass, id);
em.remove(T_to_be_deleted);
}
}

View File

@@ -1,4 +1,10 @@
package DAO;
public class QuestionDAO {
import metier.Question;
public class QuestionDAO extends GenericDAOImpl<Integer, Question> {
public QuestionDAO(){
super();
this.setClass(Question.class);
}
}

View File

@@ -1,4 +1,10 @@
package DAO;
public class QuizzDAO {
import metier.Quizz;
public class QuizzDAO extends GenericDAOImpl<Integer, Quizz>{
public QuizzDAO(){
super();
this.setClass(Quizz.class);
}
}

View File

@@ -1,5 +0,0 @@
package DAO;
public class ReponseCourteDAO {
}

View File

@@ -1,4 +1,11 @@
package DAO;
public class ReponseDAO {
import metier.Reponse;
public class ReponseDAO extends GenericDAOImpl<Integer, Reponse> {
public ReponseDAO(){
super();
this.setClass(Reponse.class);
}
}

View File

@@ -1,4 +1,12 @@
package DAO;
public class SessionDAO {
import metier.Session;
public class SessionDAO extends GenericDAOImpl<Integer, Session> {
public SessionDAO() {
super();
this.setClass(Session.class);
}
}

View File

@@ -1,14 +0,0 @@
package DAO;
import java.io.Serializable;
import java.util.List;
public interface Tous<T, Id extends Serializable> {
T create(T t);
T update(T t);
T read(Id id);
void delete(T t);
List<T> findAll();
T save(T t);
}

View File

@@ -1,31 +0,0 @@
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());
}
}
}

View File

@@ -1,4 +1,12 @@
package DAO;
public class UtilisateurDAO {
import metier.Utilisateur;
public class UtilisateurDAO extends GenericDAOImpl<Integer, Utilisateur>{
public UtilisateurDAO() {
super();
this.setClass(Utilisateur.class);
}
}