bonsoir,
j'ai crée 2 classes pour tester la relation OneToMany lorsque sauvegarde les entités j'ai une erreur . il semble que se soit un probleme de partage d'objet " ecole ne peut pas être utilisé par les 2 eleves" .
"j'ai suivi le tutoriel sur youtube en tapant "tuto hibernate 17""
voici la premiere classe :
la seconde classe :
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 com.hibernate.test; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import antlr.collections.List; @Entity public class Ecole { private int id; private java.util.List <Eleves> eleves ; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNameEcole() { return nameEcole; } public void setNameEcole(String nameEcole) { this.nameEcole = nameEcole; } private String nameEcole; @OneToMany(targetEntity=Eleves.class ,mappedBy="ecole", cascade=CascadeType.ALL, fetch=FetchType.EAGER) public java.util.List<Eleves> getEleves() { return eleves; } public void setEleves(java.util.List<Eleves> eleves) { this.eleves = eleves; } }
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 package com.hibernate.test; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Eleves { private int id; private String nom; private String prenom; private int age; private Ecole ecole; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } @ManyToOne @JoinColumn(name="eleve_id") public Ecole getEcole() { return ecole; } public void setEcole(Ecole _ecole) { ecole = _ecole; } }
voici la classe principale ou j'initialise les classes en les sauvegardant :
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 package com.hibernate.test; import java.io.Serializable; import java.sql.Connection; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import javax.naming.NamingException; import javax.naming.Reference; import org.hibernate.Cache; import org.hibernate.HibernateException; import org.hibernate.Interceptor; import org.hibernate.SessionFactory; import org.hibernate.StatelessSession; import org.hibernate.TypeHelper; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.hibernate.engine.FilterDefinition; import org.hibernate.metadata.ClassMetadata; import org.hibernate.metadata.CollectionMetadata; import org.hibernate.stat.Statistics; import org.hibernate.tool.hbm2ddl.SchemaExport; @SuppressWarnings("deprecation") public class Test { /** * @param args */ public static void main(String[] args) { Configuration config =new Configuration(); config.addAnnotatedClass(Ecole.class); config.addAnnotatedClass(Eleves.class); config.configure("hibernate.cfg.xml"); new SchemaExport(config).create(true, true); SessionFactory factory = config.buildSessionFactory(); Session session = factory.getCurrentSession(); session.beginTransaction(); Ecole ecole =new Ecole(); Ecole ecole2 =new Ecole(); ecole.setNameEcole("Nathan"); ecole2.setNameEcole("Suise"); Eleves eleve =new Eleves(); eleve.setNom("Dzar"); eleve.setPrenom("Dominique"); eleve.setAge(36); eleve.setEcole(ecole); Eleves eleve2 =new Eleves(); eleve2.setAge(27); eleve2.setNom("Pascale"); eleve2.setPrenom("Dauphin"); eleve2.setEcole(ecole); session.save(ecole); session.save(eleve); session.save(eleve2); session.getTransaction().commit(); /* List<Eleves> list= session.createSQLQuery("SELECT * from Eleves,Ecole where Ecole.id=1").list(); Iterator<Eleves> iterator; iterator = list.iterator(); while(iterator.hasNext()){ Eleves el = iterator.next(); System.out.println(el.getId()+" "+el.getPrenom()+" "+ el.getNom()); } */ } }
voici les erreurs du compilateur :
merci !
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 alter table TEST.ELEVES drop constraint FK7A0E06465914D2D2 alter table TEST.ELEVES drop constraint FK7A0E06465ED2BA81 drop table TEST.ECOLE drop table TEST.ELEVES create table TEST.ECOLE (ID integer not null, NAMEECOLE varchar(255), primary key (ID)) create table TEST.ELEVES (ID integer not null, NOM varchar(255), PRENOM varchar(255), AGE integer, ECOLE integer, idx integer, primary key (ID)) alter table TEST.ELEVES add constraint FK7A0E06465914D2D2 foreign key (ID) references TEST.ECOLE alter table TEST.ELEVES add constraint FK7A0E06465ED2BA81 foreign key (ECOLE) references TEST.ECOLE Exception in thread "main" org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.hibernate.test.Eleves#0] at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:190) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:143) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344) at $Proxy4.save(Unknown Source) at com.hibernate.test.Test.main(Test.java:77)
Partager