DAO commit
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
package DAO;
|
||||
|
||||
public class ChoixDAO {
|
||||
}
|
||||
13
src/main/java/DAO/Generic.java
Normal file
13
src/main/java/DAO/Generic.java
Normal 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();
|
||||
}
|
||||
60
src/main/java/DAO/GenericDAOImpl.java
Normal file
60
src/main/java/DAO/GenericDAOImpl.java
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package DAO;
|
||||
|
||||
public class ReponseCourteDAO {
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package jpa;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityTransaction;
|
||||
import metier.*;
|
||||
|
||||
public class JpaTest {
|
||||
|
||||
@@ -24,7 +25,7 @@ public class JpaTest {
|
||||
tx.begin();
|
||||
try {
|
||||
|
||||
// TODO create and persist entity
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -36,6 +37,24 @@ public class JpaTest {
|
||||
System.out.println(".. done");
|
||||
}
|
||||
|
||||
private void create_quizz() {
|
||||
int numQuizz = manager.createQuery("SELECT q FROM Quizz q", Quizz.class).getResultList().size();
|
||||
if (numQuizz == 0) {
|
||||
//Utilisateur
|
||||
Utilisateur utilisateur1 = new Utilisateur();
|
||||
utilisateur1.setId(1);
|
||||
utilisateur1.setName("Tibo");
|
||||
|
||||
Utilisateur utilisateur2 = new Utilisateur();
|
||||
utilisateur2.setId(2);
|
||||
utilisateur2.setName("Rochas");
|
||||
|
||||
manager.persist(utilisateur1);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -6,11 +6,6 @@ import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
public class Choix extends Reponse{
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
int id;
|
||||
|
||||
ArrayList<String> choix;
|
||||
|
||||
public Choix() {
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
package metier;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
public class Question {
|
||||
|
||||
public class Question implements Serializable {
|
||||
private int id;
|
||||
private Reponse reponse;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package metier;
|
||||
|
||||
public class Quizz extends Activity{
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Quizz extends Activity implements Serializable {
|
||||
|
||||
public Quizz(){
|
||||
super();
|
||||
|
||||
@@ -1,17 +1,28 @@
|
||||
package metier;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
public abstract class Reponse {
|
||||
@Id
|
||||
public abstract class Reponse implements Serializable {
|
||||
private int id;
|
||||
|
||||
ArrayList<String> reponses;
|
||||
|
||||
|
||||
public Reponse(){
|
||||
public Reponse(){}
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,16 +5,16 @@ import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
public class ReponseCourte extends Reponse{
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
int id;
|
||||
|
||||
String value;
|
||||
|
||||
|
||||
public ReponseCourte(){
|
||||
super();
|
||||
}
|
||||
|
||||
public void setValue(String value){
|
||||
this.value = value;
|
||||
}
|
||||
public String getValue(){
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,14 @@
|
||||
package metier;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Session {
|
||||
|
||||
@Id
|
||||
public class Session implements Serializable {
|
||||
private int codePIN;
|
||||
|
||||
@OneToMany
|
||||
private ArrayList<Activity> activities;
|
||||
|
||||
@ManyToMany
|
||||
private ArrayList<Utilisateur> utilisateurs;
|
||||
|
||||
public Session(){
|
||||
@@ -23,6 +18,8 @@ public class Session {
|
||||
this.codePIN = codePIN;
|
||||
}
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public int getCodePIN(){
|
||||
return this.codePIN;
|
||||
}
|
||||
@@ -31,6 +28,7 @@ public class Session {
|
||||
this.activities = activities;
|
||||
}
|
||||
|
||||
@OneToMany
|
||||
public ArrayList<Activity> getActivities(){
|
||||
return this.activities;
|
||||
}
|
||||
@@ -39,9 +37,8 @@ public class Session {
|
||||
this.utilisateurs = utilisateurs;
|
||||
}
|
||||
|
||||
@ManyToMany
|
||||
public ArrayList<Utilisateur> getUtilisateurs() {
|
||||
return this.utilisateurs;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -2,19 +2,15 @@ package metier;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Utilisateur{
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public class Utilisateur implements Serializable {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
@ManyToMany
|
||||
private Session session;
|
||||
|
||||
@Column(unique=true)
|
||||
private String email;
|
||||
private String password;
|
||||
|
||||
@@ -23,6 +19,8 @@ public class Utilisateur{
|
||||
|
||||
public Utilisateur() {}
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -39,6 +37,16 @@ public class Utilisateur{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Column(unique=true)
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@ManyToMany
|
||||
public Session getSession() {
|
||||
return session;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user