bonjour tout le monde !

Je rencontre un problème au quel je ne trouve pas de solution dans mon projet JPA et je sollicite votre aide !

enfait voilà le code de mon entité employee
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
75
76
77
78
79
80
81
82
package org.esprit.persistence;
 
import java.io.Serializable;
import java.lang.String;
import java.util.Date;
import javax.persistence.*;
 
/**
 * Entity implementation class for Entity: Employee
 *
 */
@Entity(name="t_employee")
 
public class Employee implements Serializable {
 
 
	private String matricule;
	private String surname;
	private String name;
	private String e_mail;
	private Date date_of_birth;
	private String login;
	private String password;
	private static final long serialVersionUID = 1L;
 
	public Employee() {
		super();
	}   
	@Id    @Column(name="pk_employee")
	public String getMatricule() {
		return this.matricule;
	}
 
	public void setMatricule(String matricule) {
		this.matricule = matricule;
	}   
	public String getSurname() {
		return this.surname;
	}
 
	public void setSurname(String surname) {
		this.surname = surname;
	}   
	public String getName() {
		return this.name;
	}
 
	public void setName(String name) {
		this.name = name;
	}   
	public String getE_mail() {
		return this.e_mail;
	}
 
	public void setE_mail(String e_mail) {
		this.e_mail = e_mail;
	}   
 
	@Temporal(TemporalType.DATE)
	public Date getDate_of_birth() {
		return this.date_of_birth;
	}
 
	public void setDate_of_birth(Date date_of_birth) {
		this.date_of_birth = date_of_birth;
	}   
	public String getLogin() {
		return this.login;
	}
 
	public void setLogin(String login) {
		this.login = login;
	}   
	public String getPassword() {
		return this.password;
	}
 
	public void setPassword(String password) {
		this.password = password;
	}
 
}
puis j'ai fait une classe utility pour la création de la entityManagerfactory :

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
 
package org.esprit.utilities;
 
import java.util.HashMap;
import java.util.Map;
 
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
 
public class JPAUtil {
 
	private static EntityManager em;
	private static EntityManagerFactory emf;
	private static Map<String, EntityManager> cache=
		new HashMap<String, EntityManager>();
 
	public static EntityManager getEm(String unitname)
	{
		if (!cache.containsKey(unitname))
		{
			emf=Persistence.createEntityManagerFactory(unitname);
			em=emf.createEntityManager();
			cache.put(unitname, em);
		}
 
		return cache.get(unitname);
	}
 
}
et enfin j'ai créé ma classe DAO pour réaliser les opérations CRUD

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
 
package org.esprit.DAOs;
 
import java.util.List;
 
import javax.persistence.EntityManager;
 
import org.esprit.persistence.Employee;
import org.esprit.utilities.JPAUtil;
 
public class EmployeeDAO {
	private EntityManager em= JPAUtil.getEm("punit1");
	private static EmployeeDAO  instance;
 
	public static EmployeeDAO getInstance() {
		if(instance==null)
		instance=new EmployeeDAO();
		return instance;
	}
		public void ajouter(Employee employee){
			em.persist(employee);
		}
		public void supprimer(String matricule){
			Employee employee= new Employee();
			employee= getEmployeeByReference(matricule);
			em.remove(employee)
;
			}
		public void modifier(Employee employee){
			em.merge(employee);
		}
		public Employee getEmployeeByReference(String matricule){
 
			return em.find(Employee.class,matricule);
			}
		public List<Employee> getAll()
		{
			return em.createQuery("From Employee").getResultList();
		}
 
}
Et biensur enfin une classe de test pour valider :

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
 
package org.esprit.tests;
 
import java.util.Date;
 
import javax.persistence.EntityTransaction;
 
import org.esprit.DAOs.EmployeeDAO;
import org.esprit.persistence.Employee;
import org.esprit.utilities.JPAUtil;
 
public class AddEmployeeDAO {
 
 
	public static void main(String[] args) {
 
		Employee employee= new Employee();
		employee.setSurname("yness");
		employee.setName("barboura");
		employee.setMatricule("025l");
		employee.setLogin("ynessb");
		employee.setPassword("yness");
		employee.setDate_of_birth(new Date("23/02/1987"));
		employee.setE_mail("yness_91@hotmail.com");
 
		Employee employee1= new Employee();
		employee1.setSurname("yness");
		employee1.setName("barboura");
		employee1.setMatricule("02l");
		employee1.setLogin("ynessb");
		employee1.setPassword("yness");
		employee1.setDate_of_birth(new Date("23/02/1987"));
		employee1.setE_mail("yness_91@hotmail.com");
 
		EmployeeDAO employeedao= EmployeeDAO.getInstance();
		EntityTransaction tx= JPAUtil.getEm("punit1").getTransaction();
		try{
			tx.begin();
			employeedao.ajouter(employee);
			employeedao.ajouter(employee1);
			tx.commit();
			}catch (Exception e) {
				tx.rollback();
				// TODO: handle exception
			}
 
	}
 
}
Quand j'execute ce dernier code tout marche bien, il insère les lignes que je lui demande... Mais mon problème quand je relance une nouvelle fois ce code biensur en changeant les données à insérer dans la table, il efface les lignes déjà créer et insère que les nouvelles... On dirait qu'à chaque fois je vide la table employee pour y insérer que les nouvelles entrée...alors que c'est pas du tt ce que je cherche ! Je ne vois pas ou le problème,donc si quelqu'un peut m'aider à voir mieux sa sra trèès aimable !