Bonjour tout le monde, je suis coîncé sur l'accès au donnée dans mon projet JPA. Apres avoir configurer la base de donnée h2 je parvient à executer les requettes sql dans le terminal dédié via eclipse.
Vous avez ici un exemple d'entité généréé
mon fichier persitence.xml est le suivant:
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 package jPAProject; import java.io.Serializable; import javax.persistence.*; import java.util.List; /** * The persistent class for the "employees" database table. * */ @Entity @Table(name="\"employees\"") @NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e") public class Employee implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="\"id\"") private int id; @Column(name="\"address\"") private String address; @Lob @Column(name="\"attachments\"") private byte[] attachments; @Column(name="\"business_phone\"") private String businessPhone; @Column(name="\"city\"") private String city; @Column(name="\"company\"") private String company; @Column(name="\"country_region\"") private String countryRegion; @Column(name="\"email_address\"") private String emailAddress; @Column(name="\"fax_number\"") private String faxNumber; @Column(name="\"first_name\"") private String firstName; @Column(name="\"home_phone\"") private String homePhone; @Column(name="\"job_title\"") private String jobTitle; @Column(name="\"last_name\"") private String lastName; @Column(name="\"mobile_phone\"") private String mobilePhone; @Column(name="\"notes\"") private String notes; @Column(name="\"state_province\"") private String stateProvince; @Column(name="\"web_page\"") private String webPage; @Column(name="\"zip_postal_code\"") private String zipPostalCode; //bi-directional many-to-one association to Order @OneToMany(mappedBy="employee") private List<Order> orders; //bi-directional many-to-many association to Privilege @ManyToMany(mappedBy="employees") private List<Privilege> privileges; //bi-directional many-to-one association to PurchaseOrder @OneToMany(mappedBy="employee") private List<PurchaseOrder> purchaseOrders; public Employee() { } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } public byte[] getAttachments() { return this.attachments; } public void setAttachments(byte[] attachments) { this.attachments = attachments; } public String getBusinessPhone() { return this.businessPhone; } public void setBusinessPhone(String businessPhone) { this.businessPhone = businessPhone; } public String getCity() { return this.city; } public void setCity(String city) { this.city = city; } public String getCompany() { return this.company; } public void setCompany(String company) { this.company = company; } public String getCountryRegion() { return this.countryRegion; } public void setCountryRegion(String countryRegion) { this.countryRegion = countryRegion; } public String getEmailAddress() { return this.emailAddress; } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } public String getFaxNumber() { return this.faxNumber; } public void setFaxNumber(String faxNumber) { this.faxNumber = faxNumber; } public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getHomePhone() { return this.homePhone; } public void setHomePhone(String homePhone) { this.homePhone = homePhone; } public String getJobTitle() { return this.jobTitle; } public void setJobTitle(String jobTitle) { this.jobTitle = jobTitle; } public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getMobilePhone() { return this.mobilePhone; } public void setMobilePhone(String mobilePhone) { this.mobilePhone = mobilePhone; } public String getNotes() { return this.notes; } public void setNotes(String notes) { this.notes = notes; } public String getStateProvince() { return this.stateProvince; } public void setStateProvince(String stateProvince) { this.stateProvince = stateProvince; } public String getWebPage() { return this.webPage; } public void setWebPage(String webPage) { this.webPage = webPage; } public String getZipPostalCode() { return this.zipPostalCode; } public void setZipPostalCode(String zipPostalCode) { this.zipPostalCode = zipPostalCode; } public List<Order> getOrders() { return this.orders; } public void setOrders(List<Order> orders) { this.orders = orders; } public Order addOrder(Order order) { getOrders().add(order); order.setEmployee(this); return order; } public Order removeOrder(Order order) { getOrders().remove(order); order.setEmployee(null); return order; } public List<Privilege> getPrivileges() { return this.privileges; } public void setPrivileges(List<Privilege> privileges) { this.privileges = privileges; } public List<PurchaseOrder> getPurchaseOrders() { return this.purchaseOrders; } public void setPurchaseOrders(List<PurchaseOrder> purchaseOrders) { this.purchaseOrders = purchaseOrders; } public PurchaseOrder addPurchaseOrder(PurchaseOrder purchaseOrder) { getPurchaseOrders().add(purchaseOrder); purchaseOrder.setEmployee(this); return purchaseOrder; } public PurchaseOrder removePurchaseOrder(PurchaseOrder purchaseOrder) { getPurchaseOrders().remove(purchaseOrder); purchaseOrder.setEmployee(null); return purchaseOrder; } //le main que moi me j'ai ajouté public static void main(String[] args) { EntityManagerFactory emf=Persistence.createEntityManagerFactory("jPAProject"); EntityManager em=emf.createEntityManager(); Query nQuery = em.createNamedQuery("Employee.findAll",Employee.class); List<Employee> customers = nQuery.getResultList(); for(Employee c:customers) System.out.println(c.getLastName()); } }
et le fichier pom.xml
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 <?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="jPAProject" transaction-type="RESOURCE_LOCAL"> <mapping-file>META-INF/querry.xml</mapping-file> <class>jPAProject.Customer</class> <class>jPAProject.Employee</class> <class>jPAProject.InventoryTransactionType</class> <class>jPAProject.InventoryTransaction</class> <class>jPAProject.Invoice</class> <class>jPAProject.OrderDetail</class> <class>jPAProject.OrderDetailsStatus</class> <class>jPAProject.Order</class> <class>jPAProject.OrdersStatus</class> <class>jPAProject.OrdersTaxStatus</class> <class>jPAProject.Privilege</class> <class>jPAProject.Product</class> <class>jPAProject.PurchaseOrderDetail</class> <class>jPAProject.PurchaseOrderStatus</class> <class>jPAProject.PurchaseOrder</class> <class>jPAProject.SalesReport</class> <class>jPAProject.Shipper</class> <class>jPAProject.Supplier</class> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:h2:/home/sylla/Documents/COURS_L3/ProjetEclipse/northwind;AUTO_SERVER=TRUE"/> <property name="javax.persistence.jdbc.user" value="sa"/> <property name="javax.persistence.jdbc.password" value=""/> <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> <property name="eclipselink.logging.level" value="FINE"/> <property name="javax.persistence.logging.level" value="FINE"/> <property name="eclipselink.allow-zero-id" value="true"/> </properties> </persistence-unit> </persistence>
et un screen de l'erreur en sortie
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 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jPAProject</groupId> <artifactId>jPAProject</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>eclipselink</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.199</version> </dependency> </dependencies> </project>
Error Code: 42102
Call: SELECT "id", "address", "attachments", "business_phone", "city", "company", "country_region", "email_address", "fax_number", "first_name", "home_phone", "job_title", "last_name", "mobile_phone", "notes", "state_province", "web_page", "zip_postal_code" FROM "employees"
Query: ReadAllQuery(name="Employee.findAll" referenceClass=Employee sql="SELECT "id", "address", "attachments", "business_phone", "city", "company", "country_region", "email_address", "fax_number", "first_name", "home_phone", "job_title", "last_name", "mobile_phone", "notes", "state_province", "web_page", "zip_postal_code" FROM "employees"")
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "employees" non trouvée
Table "employees" not found; SQL statement:
SELECT "id", "address", "attachments", "business_phone", "city", "company", "country_region", "email_address", "fax_number", "first_name", "home_phone", "job_title", "last_name", "mobile_phone", "notes", "state_province", "web_page", "zip_postal_code" FROM "employees" [42102-199]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:451)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:427)
at org.h2.message.DbException.get(DbException.java:205)
at org.h2.message.DbException.get(DbException.java:181)
at org.h2.command.Parser.readTableOrView(Parser.java:7146)
at org.h2.command.Parser.readTableFilter(Parser.java:1895)
at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:2641)
at org.h2.command.Parser.parseSelectSimple(Parser.java:2788)
at org.h2.command.Parser.parseSelectSub(Parser.java:2636)
at org.h2.command.Parser.parseSelectUnion(Parser.java:2469)
at org.h2.command.Parser.parseSelect(Parser.java:2440)
at org.h2.command.Parser.parsePrepared(Parser.java:814)
at org.h2.command.Parser.parse(Parser.java:788)
at org.h2.command.Parser.parse(Parser.java:760)
at org.h2.command.Parser.prepareCommand(Parser.java:683)
at org.h2.engine.Session.prepareLocal(Session.java:627)
at org.h2.server.TcpServerThread.process(TcpServerThread.java:270)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:175)
at java.lang.Thread.run(Thread.java:748)
Une image du shema de mes tables dans le dataSource explorer
Et la connexion à la base reussie parfaitement. Je vous remercie d'avance pour vos coups de main car j'en ai vivement besoin.
Partager