Salut,
dans un dynamique web project..
voici ma classe Personne
ma classe Personneservice :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 package com.projet.bean; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Personne implements Serializable { /** * */ private static final long serialVersionUID = 1L; @Id @GeneratedValue private int id; private String nom; private String prenom; private String email; public Personne(String nom, String prenom, String email) { super(); this.nom = nom; this.prenom = prenom; this.email = email; } public Personne() { super(); } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public String getPrenom() { return prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getId() { return id; } }
mon interface IDao:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 package com.projet.service; import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.NoResultException; import javax.persistence.Persistence; import javax.persistence.PersistenceContext; import javax.persistence.Query; import com.project.dao.IDao; import com.projet.bean.Personne; @Stateless public class PersonneService implements IDao<Personne> { private static final String JPQL_SELECT_PAR_ID = "SELECT u FROM Personne u WHERE u.id=:id"; // Injection du manager, qui s'occupe de la connexion avec la BDD private EntityManagerFactory emf= Persistence.createEntityManagerFactory("manager1"); @PersistenceContext private EntityManager em = emf.createEntityManager(); // Enregistrement d'un nouvelle personne public void create(Personne personne) { em.persist(personne); em.getTransaction().commit(); } // Recherche d'une personne à partir de son id public Personne findById(int id) { Personne personne = null; personne = em.find(Personne.class, id); return personne; } // MAJ d'une personne public void update(Personne personne) { em.refresh(personne); em.getTransaction().commit(); } // supprimer une personne public void delete(Personne personne) { Personne pers = findById(personne.getId()); if (pers != null) { em.remove(pers); em.getTransaction().commit(); } } // findALL public List<Personne> findAll() { Query query = em.createQuery("SELECT e FROM Personne e"); List<Personne> personnes = (List<Personne>) query.getResultList(); return personnes; } // Recherche d'un utilisateur à partir de son id public Personne findById2(int id) { Personne personne = null; Query requete = em.createQuery(JPQL_SELECT_PAR_ID); requete.setParameter("id", id); try { personne = (Personne) requete.getSingleResult(); } catch (NoResultException e) { return null; } return personne; } }
ma classe du teste :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 package com.project.dao; import java.util.List; public interface IDao<G> { public void create(G o); public void update(G o); public void delete(G o); public G findById(int id); public List<G> findAll(); }
mon fichier persistence.xml du JPA context :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.project.test; import com.projet.bean.Personne; import com.projet.service.PersonneService; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub PersonneService ps = new PersonneService(); Personne pers = new Personne(); pers.setEmail("fussa.fyby@ymail.com"); pers.setNom("Fyby"); pers.setPrenom("FuSsA"); ps.create(pers); } }
j'utilise GlassFich
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL"> <class>com.projet.bean.Personne</class> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/tp2_personne"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value=""/> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> </properties> </persistence-unit> </persistence>
lorsque j clique sur "run on server" de puis ma classe test.. ya aucun changement au niveau de ma base de données !
merci pour votre aide..
Partager