Hello,
J'ai un petit soucies avec des clefs primaires composés et l'utilisation depuis une Entity qui possède une relation étrangère.
Voici mon message d'erreur :
Voici la structure de mes tables incriminées :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 The @JoinColumns on the annotated element [private com.ejb.entity.core.Message com.ejb.entity.core.Country.message] from the entity class [class com.ejb.entity.core.Country] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referenceColumnName elements must be specified in each such @JoinColumn.
La clef primaire de message est donc définit par id_message et ref_lang
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 DROP TABLE IF EXISTS `message`; CREATE TABLE `message` ( `id_message` int NOT NULL, `text` text NOT NULL, `ref_type` int(4) NOT NULL, `ref_lang` int(2) NOT NULL, FOREIGN KEY `FK_MESSAGE_TYPE` (`ref_type`) REFERENCES `type` (`id_type`), PRIMARY KEY (`id_message`,`ref_lang`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; DROP TABLE IF EXISTS `country`; CREATE TABLE `country` ( `id_country` int(11) NOT NULL, `ref_message` int(4) NOT NULL, `country_code` char(2) NOT NULL, PRIMARY KEY (`id_country`,`ref_message`), FOREIGN KEY `FK_country_MESSAGE` (`ref_message`) REFERENCES `message` (`id_message`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Les EJB donnes donc, pour message :
Et pour Country :
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 package com.ejb.entity.core; import java.io.Serializable; import java.util.Collection; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.Lob; import javax.persistence.ManyToOne; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToMany; import javax.persistence.Table; /** * Entity class Message * * @author alex */ @Entity @Table(name = "message") @NamedQueries( { @NamedQuery(name = "Message.findByIdMessage", query = "SELECT m FROM Message m WHERE m.messagePK.idMessage = :idMessage"), @NamedQuery(name = "Message.findByLang", query = "SELECT m FROM Message m WHERE m.messagePK.lang = :lang") }) public class Message implements Serializable { /** * EmbeddedId primary key field */ @EmbeddedId protected MessagePK messagePK; @Lob @Column(name = "text", nullable = false) private String text; @OneToMany(cascade = CascadeType.ALL, mappedBy = "message") private Collection<Month> monthCollection; @OneToMany(cascade = CascadeType.ALL, mappedBy = "message") private Collection<Country> countryCollection; @JoinColumn(name = "ref_type", referencedColumnName = "id_type") @ManyToOne private Type refType; @OneToMany(cascade = CascadeType.ALL, mappedBy = "message") private Collection<Category> categoryCollection; /** Creates a new instance of Message */ public Message() { } /** * Creates a new instance of Message with the specified values. * @param messagePK the messagePK of the Message */ public Message(MessagePK messagePK) { this.messagePK = messagePK; } /** * Creates a new instance of Message with the specified values. * @param messagePK the messagePK of the Message * @param text the text of the Message */ public Message(MessagePK messagePK, String text) { this.messagePK = messagePK; this.text = text; } /** * Creates a new instance of MessagePK with the specified values. * @param lang the lang of the MessagePK * @param idMessage the idMessage of the MessagePK */ public Message(int lang, int idMessage) { this.messagePK = new MessagePK(lang, idMessage); } /** * Gets the messagePK of this Message. * @return the messagePK */ public MessagePK getMessagePK() { return this.messagePK; } /** * Sets the messagePK of this Message to the specified value. * @param messagePK the new messagePK */ public void setMessagePK(MessagePK messagePK) { this.messagePK = messagePK; } /** * Gets the text of this Message. * @return the text */ public String getText() { return this.text; } /** * Sets the text of this Message to the specified value. * @param text the new text */ public void setText(String text) { this.text = text; } /** * Gets the monthCollection of this Message. * @return the monthCollection */ public Collection<Month> getMonthCollection() { return this.monthCollection; } /** * Sets the monthCollection of this Message to the specified value. * @param monthCollection the new monthCollection */ public void setMonthCollection(Collection<Month> monthCollection) { this.monthCollection = monthCollection; } /** * Gets the countryCollection of this Message. * @return the countryCollection */ public Collection<Country> getCountryCollection() { return this.countryCollection; } /** * Sets the countryCollection of this Message to the specified value. * @param countryCollection the new countryCollection */ public void setCountryCollection(Collection<Country> countryCollection) { this.countryCollection = countryCollection; } /** * Gets the refType of this Message. * @return the refType */ public Type getRefType() { return this.refType; } /** * Sets the refType of this Message to the specified value. * @param refType the new refType */ public void setRefType(Type refType) { this.refType = refType; } /** * Gets the categoryCollection of this Message. * @return the categoryCollection */ public Collection<Category> getCategoryCollection() { return this.categoryCollection; } /** * Sets the categoryCollection of this Message to the specified value. * @param categoryCollection the new categoryCollection */ public void setCategoryCollection(Collection<Category> categoryCollection) { this.categoryCollection = categoryCollection; } /** * Returns a hash code value for the object. This implementation computes * a hash code value based on the id fields in this object. * @return a hash code value for this object. */ @Override public int hashCode() { int hash = 0; hash += (this.messagePK != null ? this.messagePK.hashCode() : 0); return hash; } /** * Determines whether another object is equal to this Message. The result is * <code>true</code> if and only if the argument is not null and is a Message object that * has the same id field values as this object. * @param object the reference object with which to compare * @return <code>true</code> if this object is the same as the argument; * <code>false</code> otherwise. */ @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Message)) { return false; } Message other = (Message)object; if (this.messagePK != other.messagePK && (this.messagePK == null || !this.messagePK.equals(other.messagePK))) return false; return true; } /** * Returns a string representation of the object. This implementation constructs * that representation based on the id fields. * @return a string representation of the object. */ @Override public String toString() { return "com.ejb.entity.core.Message[messagePK=" + messagePK + "]"; } }
Si je comprend bien la partie
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 package com.ejb.entity.core; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; /** * Entity class Country * * @author alex */ @Entity @Table(name = "country") @NamedQueries( { @NamedQuery(name = "Country.findByIdCountry", query = "SELECT c FROM Country c WHERE c.countryPK.idCountry = :idCountry"), @NamedQuery(name = "Country.findByRefMessage", query = "SELECT c FROM Country c WHERE c.countryPK.refMessage = :refMessage"), @NamedQuery(name = "Country.findByCountryCode", query = "SELECT c FROM Country c WHERE c.countryCode = :countryCode") }) public class Country implements Serializable { /** * EmbeddedId primary key field */ @EmbeddedId protected CountryPK countryPK; @Column(name = "country_code", nullable = false) private String countryCode; @JoinColumn(name = "ref_message", referencedColumnName = "id_message", insertable = false, updatable = false) @ManyToOne private Message message; /** Creates a new instance of Country */ public Country() { } /** * Creates a new instance of Country with the specified values. * @param countryPK the countryPK of the Country */ public Country(CountryPK countryPK) { this.countryPK = countryPK; } /** * Creates a new instance of Country with the specified values. * @param countryPK the countryPK of the Country * @param countryCode the countryCode of the Country */ public Country(CountryPK countryPK, String countryCode) { this.countryPK = countryPK; this.countryCode = countryCode; } /** * Creates a new instance of CountryPK with the specified values. * @param refMessage the refMessage of the CountryPK * @param idCountry the idCountry of the CountryPK */ public Country(int refMessage, int idCountry) { this.countryPK = new CountryPK(refMessage, idCountry); } /** * Gets the countryPK of this Country. * @return the countryPK */ public CountryPK getCountryPK() { return this.countryPK; } /** * Sets the countryPK of this Country to the specified value. * @param countryPK the new countryPK */ public void setCountryPK(CountryPK countryPK) { this.countryPK = countryPK; } /** * Gets the countryCode of this Country. * @return the countryCode */ public String getCountryCode() { return this.countryCode; } /** * Sets the countryCode of this Country to the specified value. * @param countryCode the new countryCode */ public void setCountryCode(String countryCode) { this.countryCode = countryCode; } /** * Gets the message of this Country. * @return the message */ public Message getMessage() { return this.message; } /** * Sets the message of this Country to the specified value. * @param message the new message */ public void setMessage(Message message) { this.message = message; } /** * Returns a hash code value for the object. This implementation computes * a hash code value based on the id fields in this object. * @return a hash code value for this object. */ @Override public int hashCode() { int hash = 0; hash += (this.countryPK != null ? this.countryPK.hashCode() : 0); return hash; } /** * Determines whether another object is equal to this Country. The result is * <code>true</code> if and only if the argument is not null and is a Country object that * has the same id field values as this object. * @param object the reference object with which to compare * @return <code>true</code> if this object is the same as the argument; * <code>false</code> otherwise. */ @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Country)) { return false; } Country other = (Country)object; if (this.countryPK != other.countryPK && (this.countryPK == null || !this.countryPK.equals(other.countryPK))) return false; return true; } /** * Returns a string representation of the object. This implementation constructs * that representation based on the id fields. * @return a string representation of the object. */ @Override public String toString() { return "com.ejb.entity.core.Country[countryPK=" + countryPK + "]"; } }
Cela signifie donc que je devrais non plus définir mon join de cette manièrecom.ejb.entity.core.Message com.ejb.entity.core.Country.message] from the entity class [class com.ejb.entity.core.Country] is incomplete.
mais de cette manière
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 @JoinColumn(name = "ref_message", referencedColumnName = "", insertable = false, updatable = false) @ManyToOne
J'arrive enfin au problème : Je ne possède pas dans mon Entité Coutry de champ ref_lang.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 @JoinColumns({ @JoinColumn(name="ref_message", referencedColumnName="id_message"), @JoinColumn(name="ref_lang", referencedColumnName="ref_lang") })
Comment faire :/
Partager