Client / Salarie / Authentification
This commit is contained in:
50
src/main/java/com/test/bdd/SalarieJPA.java
Normal file
50
src/main/java/com/test/bdd/SalarieJPA.java
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||||
|
*/
|
||||||
|
package com.test.bdd;
|
||||||
|
|
||||||
|
import com.test.beans.Salarie;
|
||||||
|
import jakarta.ejb.Stateless;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author steve.maingana
|
||||||
|
*/
|
||||||
|
@Stateless
|
||||||
|
public class SalarieJPA {
|
||||||
|
@PersistenceContext(unitName = "bdclientPU") //en lien avec le fichier persistence.xml : <persistence-unit name="bdclientPU" transaction-type="JTA">
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
// Mise à jour d'un client
|
||||||
|
public int update(Salarie salarie) {
|
||||||
|
Salarie leSalarie = em.merge(salarie);
|
||||||
|
em.flush();
|
||||||
|
return leSalarie.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lecture de tous les clients
|
||||||
|
public List<Salarie> readAll() {
|
||||||
|
List<Salarie> lesSalaries = em.createQuery("SELECT s FROM Salarie s", Salarie.class).getResultList();
|
||||||
|
return lesSalaries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Salarie readSalarie(String login, String mdp) {
|
||||||
|
TypedQuery<Salarie> query = em.createQuery("SELECT s FROM Salarie s WHERE s.login = ?1 AND s.mdp= ?2 LIMIT 1", Salarie.class);
|
||||||
|
Salarie salarie = query.setParameter(1, login)
|
||||||
|
.setParameter(2, mdp)
|
||||||
|
.getSingleResult();
|
||||||
|
|
||||||
|
return salarie;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Création d’un client
|
||||||
|
public int create(Salarie unSalarie) {
|
||||||
|
em.persist(unSalarie); // enregistre l’objet en base
|
||||||
|
// Force la synchro avec la base pour récupérer l’ID tout de suite
|
||||||
|
em.flush();
|
||||||
|
return unSalarie.getId(); // l’ID est rempli automatiquement par JPA
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user