Salut,
J'ai une application web qui permet d'ajouter des documents dans une base de données, donc le nom, la date de création, l'emplacement, et une liste de modification. Je travail avec les EJB3, JBoss et postgresql.
Tout d'abord, j'ai créé une classe document qui représente la table document dans la base de données, dont voici le code :
Vous pouvez voir en rouge qu'il y a la liste de modification donc il y a une ralation OneToMany
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 @Entity public class Document implements Serializable{ // les variables correspondent aux colonnes de la table Document de la base de données @Column(name = "nom", nullable = false) private String nom; // le nom du document @Column(name = "date_creation", nullable = false) private java.util.Date date_creation; // la date de création du document @Column(name = "emplacement", nullable = false) private String emplacement; // l'emplacement du document @Column(name = "modification", nullable = false) private java.util.Set<Modification> modification; // la date de dernière modification @Column(name = "tag", nullable = false) private java.util.Set<Tag> tag; // la liste des tags représentant ce document private static final long serialVersionUID = -728268157L; private Long id; // l'identifiant obligatoire pour la table @Column(name = "typeDocument", nullable = false) private TypeDocument typeDocument; // le type du document : ex technique, commercial .. // le constructeur qui est vide public Document() { super(); } // redéfinition de la méthode ToString public String toString() { return "Document" + " nom=" + nom + " date_creation=" + date_creation + " emplacement=" + emplacement + " id=" + id; } // les getters et setters public String getNom() { return this.nom; } public void setNom(String nom) { this.nom = nom; } public java.util.Date getDate_creation() { return this.date_creation; } public void setDate_creation(java.util.Date date_creation) { this.date_creation = date_creation; } public String getEmplacement() { return this.emplacement; } public void setEmplacement(String emplacement) { this.emplacement = emplacement; } @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "document") public Collection<Modification> getModification() { if (modification == null) { modification = new java.util.HashSet<Modification>(); } return modification; } public void setModification(java.util.Set<Modification> modification) { this.modification = modification; } public void addModification(Modification Modification) { getModification().add(Modification); } public void removeModification(Modification Modification) { getModification().remove(Modification); } @ManyToMany(cascade=CascadeType.ALL, mappedBy="document", targetEntity=Tag.class) public java.util.Set<Tag> getTag() { if (tag == null) { tag = new java.util.HashSet<Tag>(); } return tag; } public void setTag(java.util.Set<Tag> tag) { this.tag = tag; } public void addTag(Tag tag) { getTag().add(tag); } public void removeTag(Tag tag) { getTag().remove(tag); } @Id // tag définissant la clé primaire @GeneratedValue(strategy=GenerationType.AUTO) // tag indiquant que la clé est auto générée public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } @ManyToOne @JoinColumn(name = "typeDocumentID") public TypeDocument getTypeDocument() { return this.typeDocument; } public void setTypeDocument(TypeDocument typeDocument) { this.typeDocument = typeDocument; } }
Voici maintenant le code de la classe modification :
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 @Entity public class Modification implements Serializable { // les variables correspondent aux colonnes de la table Modification de la base de données @Column(name = "date", nullable = false) private java.util.Date date; // la date de modification @Column(name = "commentaire", nullable = false) private String commentaire; // le commentaire pour expliqué ce qu'il a été fait comme modification @Column(name = "documentID", nullable = false) private Document document; // représente le document qui a été modifié private Utilisateur user; // représente le user qui a fait cette modification private static final long serialVersionUID = 258313988L; private Long id; // identifiant obligatoire pour la table // redéfinition de la méthode toString public String toString() { return "Modification" + " date=" + date + " commentaire=" + commentaire + " id=" + id; } // le constructeur vide public Modification() { super(); } // les getters et les setters public java.util.Date getDate() { return this.date; } public void setDate(java.util.Date date) { this.date = date; } public String getCommentaire() { return this.commentaire; } public void setCommentaire(String commentaire) { this.commentaire = commentaire; } //@ManyToOne(optional=false) @ManyToOne @JoinColumn(name = "documentID") public Document getDocument() { return this.document; } public void setDocument(Document document) { this.document = document; } @ManyToOne @JoinColumn(name = "UserID") public Utilisateur getUser() { return this.user; } public void setUser(Utilisateur user) { this.user = user; } @Id // tag définissant la clé primaire @GeneratedValue(strategy=GenerationType.AUTO) // tag indiquant que la clé est auto générée public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } }
Maintenat, côté servlet je veux ajouter un nouveau document, donc dans le code je cré un document et j'appel la méthode qui se trouve dans mon session bean qui permet de faire cela.
Voici le code de ma servlet
j'ai mis en rouge les parties importante
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 package servlet; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.StringTokenizer; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ejb.Document; import ejb.GestionDeDocument; import ejb.Modification; import ejb.Tag; import ejb.TypeDocument; import ejb.Utilisateur; public class AjouterDocument extends HttpServlet{ private String nom; private Date date_creation; private String emplacement; private String listeTags; private Set<Tag> tags ; private String leType; private TypeDocument type; private Set<Modification> listeModification; private Set<Document> listeDocument; // cette méthode permet de récupérer la requête envoyé par l'applet et de renvoyer la réponse public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ // instantiation d'un nouveau document Document document = new Document(); // récupération des paramètres envoyés par l'applet // le nom du document nom = request.getParameter("nom"); //l'emplacement emplacement = request.getParameter("emplacement"); // la liste des tags listeTags = request.getParameter("tags"); // la liste de tags envoyée par l'applet est du style tag1|tag2|tag3 // on récupère les tags un par un tags = new HashSet<Tag>(); StringTokenizer lesTags = new StringTokenizer(listeTags); while(lesTags.hasMoreTokens()){ Tag t = new Tag(); String nomTag = lesTags.nextToken(); t.setNom(nomTag); t.setCommentaire(""); tags.add(t); // on ajoute le tag dans la liste } // le type du document leType = request.getParameter("type"); type = new TypeDocument(); type.setNom(leType); type.setCommentaire(""); // la date de création date_creation = new Date(); // modification listeModification = new HashSet<Modification>(); /* Modification modif = new Modification(); modif.setCommentaire("ajout"); modif.setDate(date_creation); modif.setDocument(document); Utilisateur user = new Utilisateur(); user.setInitiale("YB"); user.setMotDePasse("motDePasse"); user.setNom("Bardon"); user.setPrenom("Yannick"); user.setModification(listeModification); modif.setUser(user); */ // on construit le nouveau document avec tous les paramètres document.setDate_creation(date_creation); document.setEmplacement(emplacement); document.setModification(listeModification); document.setNom(nom); document.setTag(tags); document.setTypeDocument(type); // le type correspond à plusieurs document donc c'est pour cela qu'on lui met une liste de document listeDocument = new HashSet<Document>(); listeDocument.add(document); type.setDocument(listeDocument); // on va faire appel aux ejb try { Context context = new InitialContext(); GestionDeDocument gdd = (GestionDeDocument) context.lookup("GestionDeDocumentBean/remote"); // ajout d'un document dans la base de données gdd.ajouterDocument(document); // ajout du tag dans la base de données // le tag correspond à plusieurs document donc c'est pour cele qu'on lui met une liste de document Iterator<Tag> iterator = tags.iterator(); while(iterator.hasNext()){ Tag t = iterator.next(); t.setDocument(listeDocument); gdd.ajouterTag(t); } // ajout du type dans la base de données gdd.ajouterType(type); /*gdd.ajouterModification(modif); gdd.ajouterUser(user); */ } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Enfin une fois que je lance mon appli web via firefox, voici l'erreur que j'ai :
type Rapport d'exception
message
Je ne sais pas d'où cela peut venirdescription Le serveur a rencontré une erreur interne () qui l'a empêché de satisfaire la requête.
exception
javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of ejb.Document.modification
org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:63)
org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:106)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:278)
org.jboss.ejb3.remoting.IsLocalInterceptor.invokeLocal(IsLocalInterceptor.java:79)
org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:70)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:103)
$Proxy81.ajouterDocument(Unknown Source)
servlet.AjouterDocument.doGet(AjouterDocument.java:116)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
cause mère
javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of ejb.Document.modification
org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)
org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
org.jboss.ejb3.entity.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:182)
ejb.GestionDeDocumentBean.ajouterDocument(GestionDeDocumentBean.java:32)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:106)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:278)
org.jboss.ejb3.remoting.IsLocalInterceptor.invokeLocal(IsLocalInterceptor.java:79)
org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:70)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:103)
$Proxy81.ajouterDocument(Unknown Source)
servlet.AjouterDocument.doGet(AjouterDocument.java:116)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
Merci pour vos réponses
Partager