質問編集履歴

5

エラー内容の変更

2016/07/08 01:04

投稿

Hiroaki-Yamada
Hiroaki-Yamada

スコア25

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- java.lang.IllegalArgumentException: Sources must not be empty
5
+ org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of autowired dependencies failed;nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.yamada.springboot.MyDataRepository com.yamada.springboot.HelloController.repository;No bean named 'entityManagerFactory' is defined
6
6
 
7
7
 
8
8
 

4

コードの修正

2016/07/08 01:04

投稿

Hiroaki-Yamada
Hiroaki-Yamada

スコア25

test CHANGED
File without changes
test CHANGED
@@ -2,9 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'heloController':
5
+ java.lang.IllegalArgumentException: Sources must not be empty
6
-
7
- Injection of autowired dependencies failed;Error creating bean with name 'myDataRepository':No bean named 'entityManagerFactory' is defined
8
6
 
9
7
 
10
8
 
@@ -34,40 +32,62 @@
34
32
 
35
33
  import javax.persistence.Table;
36
34
 
35
+ import javax.validation.constraints.Max;
36
+
37
+ import javax.validation.constraints.Min;
38
+
39
+ import javax.validation.constraints.NotNull;
40
+
41
+
42
+
43
+ import org.hibernate.validator.constraints.Email;
44
+
45
+ import org.hibernate.validator.constraints.NotEmpty;
46
+
37
47
 
38
48
 
39
49
  @Entity
40
50
 
41
51
  @Table(name="mydata")
42
52
 
43
-
44
-
45
53
  public class MyData {
46
54
 
55
+
56
+
47
57
  @Id
48
58
 
49
59
  @GeneratedValue(strategy = GenerationType.AUTO)
50
60
 
51
61
  @Column
52
62
 
63
+ @NotNull
64
+
53
65
  private long id;
54
66
 
55
67
 
56
68
 
57
69
  @Column(length = 50, nullable = false)
58
70
 
71
+ @NotEmpty
72
+
59
73
  private String name;
60
74
 
61
75
 
62
76
 
63
77
  @Column(length = 200, nullable = true)
64
78
 
79
+ @Email
80
+
65
81
  private String mail;
66
82
 
67
83
 
68
84
 
69
85
  @Column(nullable = true)
70
86
 
87
+ @Min(0)
88
+
89
+ @Max(200)
90
+
71
91
  private Integer age;
72
92
 
73
93
 
@@ -78,31 +98,27 @@
78
98
 
79
99
 
80
100
 
81
- public long getId(){
101
+ public long getId() {
82
102
 
83
103
  return id;
84
104
 
85
105
  }
86
106
 
87
-
88
-
89
- public void setId(long id){
107
+ public void setId(long id) {
90
-
108
+
91
- this.id =id;
109
+ this.id = id;
92
-
110
+
93
- }
111
+ }
94
-
95
-
96
-
112
+
113
+
114
+
97
- public String getName(){
115
+ public String getName() {
98
116
 
99
117
  return name;
100
118
 
101
119
  }
102
120
 
103
-
104
-
105
- public void setName(String name){
121
+ public void setName(String name) {
106
122
 
107
123
  this.name = name;
108
124
 
@@ -110,15 +126,13 @@
110
126
 
111
127
 
112
128
 
113
- public String getMail(){
129
+ public String getMail() {
114
130
 
115
131
  return mail;
116
132
 
117
133
  }
118
134
 
119
-
120
-
121
- public void setMail(String mail){
135
+ public void setMail(String mail) {
122
136
 
123
137
  this.mail = mail;
124
138
 
@@ -126,15 +140,13 @@
126
140
 
127
141
 
128
142
 
129
- public Integer getAge(){
143
+ public Integer getAge() {
130
144
 
131
145
  return age;
132
146
 
133
147
  }
134
148
 
135
-
136
-
137
- public void setAge(Integer age){
149
+ public void setAge(Integer age) {
138
150
 
139
151
  this.age = age;
140
152
 
@@ -142,15 +154,13 @@
142
154
 
143
155
 
144
156
 
145
- public String getMemo(){
157
+ public String getMemo() {
146
158
 
147
159
  return memo;
148
160
 
149
161
  }
150
162
 
151
-
152
-
153
- public void setMemo(String memo){
163
+ public void setMemo(String memo) {
154
164
 
155
165
  this.memo = memo;
156
166
 
@@ -158,8 +168,6 @@
158
168
 
159
169
  }
160
170
 
161
-
162
-
163
171
  ```
164
172
 
165
173
  ```java
@@ -168,17 +176,41 @@
168
176
 
169
177
 
170
178
 
179
+ import java.util.List;
180
+
181
+
182
+
183
+ import org.springframework.data.jpa.repository.JpaRepository;
184
+
185
+ import org.springframework.stereotype.Repository;
186
+
187
+
188
+
171
189
  import com.yamada.springboot.MyData;
172
190
 
173
- import org.springframework.data.jpa.repository.JpaRepository;
174
-
175
- import org.springframework.stereotype.Repository;
176
-
177
191
 
178
192
 
179
193
  @Repository
180
194
 
181
- public interface MyDataRepository extends JpaRepository<MyData, Long>{}
195
+ public interface MyDataRepository extends JpaRepository<MyData, Long> {
196
+
197
+
198
+
199
+ public MyData findById(Long name);
200
+
201
+ public List<MyData> findByNameLike(String name);
202
+
203
+ public List<MyData> findByIdIsNotNullOrderByIdDesc();
204
+
205
+ public List<MyData> findByAgeGreaterThan(Integer age);
206
+
207
+ public List<MyData> findByAgeBetween(Integer age1, Integer age2);
208
+
209
+
210
+
211
+ }
212
+
213
+
182
214
 
183
215
  ```
184
216
 
@@ -188,21 +220,41 @@
188
220
 
189
221
 
190
222
 
223
+ import javax.annotation.PostConstruct;
224
+
225
+
226
+
191
227
  import org.springframework.beans.factory.annotation.Autowired;
192
228
 
193
229
  import org.springframework.stereotype.Controller;
194
230
 
231
+ import org.springframework.transaction.annotation.Transactional;
232
+
233
+ import org.springframework.validation.BindingResult;
234
+
235
+ import org.springframework.validation.annotation.Validated;
236
+
237
+ import org.springframework.web.bind.annotation.ModelAttribute;
238
+
239
+ import org.springframework.web.bind.annotation.PathVariable;
240
+
195
241
  import org.springframework.web.bind.annotation.RequestMapping;
196
242
 
243
+ import org.springframework.web.bind.annotation.RequestMethod;
244
+
245
+ import org.springframework.web.bind.annotation.RequestParam;
246
+
197
247
  import org.springframework.web.servlet.ModelAndView;
198
248
 
249
+
250
+
199
251
  import com.yamada.springboot.repositories.MyDataRepository;
200
252
 
201
253
 
202
254
 
203
255
  @Controller
204
256
 
205
- public class HeloController {
257
+ public class HelloController {
206
258
 
207
259
 
208
260
 
@@ -212,176 +264,326 @@
212
264
 
213
265
 
214
266
 
215
- @RequestMapping("/")
267
+ @RequestMapping(value = "/", method = RequestMethod.GET)
216
-
268
+
217
- public ModelAndView index(ModelAndView mav){
269
+ public ModelAndView index(
270
+
271
+ @ModelAttribute("formModel") MyData mydata,
272
+
273
+ ModelAndView mav) {
218
274
 
219
275
  mav.setViewName("index");
220
276
 
221
- mav.addObject("msg", "this is sample content.");
277
+ mav.addObject("msg","this is sample content.");
278
+
279
+ mav.addObject("formModel",mydata);
222
280
 
223
281
  Iterable<MyData> list = repository.findAll();
224
282
 
225
- mav.addObject("data", list);
283
+ mav.addObject("datalist",list);
226
284
 
227
285
  return mav;
228
286
 
229
287
  }
230
288
 
289
+
290
+
291
+ @RequestMapping(value = "/", method = RequestMethod.POST)
292
+
293
+ @Transactional(readOnly=false)
294
+
295
+ public ModelAndView form(
296
+
297
+ @ModelAttribute("formModel")
298
+
299
+ @Validated MyData mydata,
300
+
301
+ BindingResult result,
302
+
303
+ ModelAndView mov) {
304
+
305
+ ModelAndView res = null;
306
+
307
+ if (!result.hasErrors()){
308
+
309
+ repository.saveAndFlush(mydata);
310
+
311
+ res = new ModelAndView("redirect:/");
312
+
313
+ } else {
314
+
315
+ mov.setViewName("index");
316
+
317
+ mov.addObject("msg","sorry, error is occured...");
318
+
319
+ Iterable<MyData> list = repository.findAll();
320
+
321
+ mov.addObject("datalist",list);
322
+
323
+ res = mov;
324
+
325
+ }
326
+
327
+ return res;
328
+
329
+ }
330
+
331
+
332
+
333
+ @PostConstruct
334
+
335
+ public void init(){
336
+
337
+ MyData d1 = new MyData();
338
+
339
+ d1.setName("tuyano");
340
+
341
+ d1.setAge(123);
342
+
343
+ d1.setMail("syoda@tuyano.com");
344
+
345
+ d1.setMemo("this is my data!");
346
+
347
+ repository.saveAndFlush(d1);
348
+
349
+ MyData d2 = new MyData();
350
+
351
+ d2.setName("hanako");
352
+
353
+ d2.setAge(15);
354
+
355
+ d2.setMail("hanako@flower");
356
+
357
+ d2.setMemo("my girl friend.");
358
+
359
+ repository.saveAndFlush(d2);
360
+
361
+ MyData d3 = new MyData();
362
+
363
+ d3.setName("sachiko");
364
+
365
+ d3.setAge(37);
366
+
367
+ d3.setMail("sachico@happy");
368
+
369
+ d3.setMemo("my work friend...");
370
+
371
+ repository.saveAndFlush(d3);
372
+
373
+ }
374
+
375
+
376
+
377
+ @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
378
+
379
+ public ModelAndView edit(@ModelAttribute MyData mydata,
380
+
381
+ @PathVariable int id,ModelAndView mav) {
382
+
383
+ mav.setViewName("edit");
384
+
385
+ mav.addObject("title","edit mydata.");
386
+
387
+ MyData data = repository.findById((long)id);
388
+
389
+ mav.addObject("formModel",data);
390
+
391
+ return mav;
392
+
393
+ }
394
+
395
+
396
+
397
+ @RequestMapping(value = "/edit", method = RequestMethod.POST)
398
+
399
+ @Transactional(readOnly=false)
400
+
401
+ public ModelAndView update(@ModelAttribute MyData mydata,
402
+
403
+ ModelAndView mav) {
404
+
405
+ repository.saveAndFlush(mydata);
406
+
407
+ return new ModelAndView("redirect:/");
408
+
409
+ }
410
+
411
+
412
+
413
+ @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
414
+
415
+ public ModelAndView delete(@PathVariable int id,
416
+
417
+ ModelAndView mav) {
418
+
419
+ mav.setViewName("delete");
420
+
421
+ mav.addObject("title","delete mydata.");
422
+
423
+ MyData data = repository.findById((long)id);
424
+
425
+ mav.addObject("formModel",data);
426
+
427
+ return mav;
428
+
429
+ }
430
+
431
+
432
+
433
+ @RequestMapping(value = "/delete", method = RequestMethod.POST)
434
+
435
+ @Transactional(readOnly=false)
436
+
437
+ public ModelAndView remove(@RequestParam long id,
438
+
439
+ ModelAndView mav) {
440
+
441
+ repository.delete(id);
442
+
443
+ return new ModelAndView("redirect:/");
444
+
445
+ }
446
+
231
447
  }
232
448
 
449
+
450
+
233
451
  ```
234
452
 
235
- ```java
236
-
237
- package com.yamada.springboot;
238
-
239
-
240
-
241
- import org.springframework.boot.SpringApplication;
242
-
243
- import org.springframework.boot.autoconfigure.SpringBootApplication;
244
-
245
-
246
-
247
- @SpringBootApplication
248
-
249
- public class MyBootAppApplication {
250
-
251
-
252
-
253
- public static void main(String[] args) {
254
-
255
- SpringApplication.run(MyBootAppApplication.class, args);
256
-
257
- }
258
-
259
- }
453
+ ```xml
454
+
455
+ <?xml version="1.0" encoding="UTF-8"?>
456
+
457
+ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
458
+
459
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
460
+
461
+ <modelVersion>4.0.0</modelVersion>
462
+
463
+
464
+
465
+ <groupId>com.yamada.springboot</groupId>
466
+
467
+ <artifactId>MyBootApp</artifactId>
468
+
469
+ <version>0.0.1-SNAPSHOT</version>
470
+
471
+ <packaging>jar</packaging>
472
+
473
+
474
+
475
+ <name>MyBootApp</name>
476
+
477
+ <description>Sample project for Spring Boot</description>
478
+
479
+
480
+
481
+ <parent>
482
+
483
+ <groupId>org.springframework.boot</groupId>
484
+
485
+ <artifactId>spring-boot-starter-parent</artifactId>
486
+
487
+ <version>1.3.6.RELEASE</version>
488
+
489
+ <relativePath/> <!-- lookup parent from repository -->
490
+
491
+ </parent>
492
+
493
+
494
+
495
+ <properties>
496
+
497
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
498
+
499
+ <java.version>1.8</java.version>
500
+
501
+ <start-class>org.springframework.boot.SpringApplication</start-class>
502
+
503
+ </properties>
504
+
505
+
506
+
507
+ <dependencies>
508
+
509
+ <dependency>
510
+
511
+ <groupId>org.springframework.boot</groupId>
512
+
513
+ <artifactId>spring-boot-starter-web</artifactId>
514
+
515
+ </dependency>
516
+
517
+
518
+
519
+ <dependency>
520
+
521
+ <groupId>org.springframework.boot</groupId>
522
+
523
+ <artifactId>spring-boot-starter-test</artifactId>
524
+
525
+ <scope>test</scope>
526
+
527
+ </dependency>
528
+
529
+ <dependency>
530
+
531
+ <groupId>org.springframework.boot</groupId>
532
+
533
+ <artifactId>spring-boot-starter-thymeleaf</artifactId>
534
+
535
+ </dependency>
536
+
537
+ <dependency>
538
+
539
+ <groupId>org.springframework.boot</groupId>
540
+
541
+ <artifactId>spring-boot-starter-data-jpa</artifactId>
542
+
543
+ </dependency>
544
+
545
+ <dependency>
546
+
547
+ <groupId>org.hsqldb</groupId>
548
+
549
+ <artifactId>hsqldb</artifactId>
550
+
551
+ <scope>runtime</scope>
552
+
553
+ </dependency>
554
+
555
+ <dependency>
556
+
557
+ <groupId>org.springframework.boot</groupId>
558
+
559
+ <artifactId>spring-boot-starter-data-mongodb</artifactId>
560
+
561
+ </dependency>
562
+
563
+ </dependencies>
564
+
565
+
566
+
567
+ <build>
568
+
569
+ <plugins>
570
+
571
+ <plugin>
572
+
573
+ <groupId>org.springframework.boot</groupId>
574
+
575
+ <artifactId>spring-boot-maven-plugin</artifactId>
576
+
577
+ </plugin>
578
+
579
+ </plugins>
580
+
581
+ </build>
582
+
583
+
584
+
585
+ </project>
260
586
 
261
587
  ```
262
588
 
263
- ```xml
264
-
265
- <?xml version="1.0" encoding="UTF-8"?>
266
-
267
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
268
-
269
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
270
-
271
- <modelVersion>4.0.0</modelVersion>
272
-
273
-
274
-
275
- <groupId>com.yamada.springboot</groupId>
276
-
277
- <artifactId>MyBootApp</artifactId>
278
-
279
- <version>0.0.1-SNAPSHOT</version>
280
-
281
- <packaging>jar</packaging>
282
-
283
-
284
-
285
- <name>MyBootApp</name>
286
-
287
- <description>sample project for Spring Boot</description>
288
-
289
-
290
-
291
- <parent>
292
-
293
- <groupId>org.springframework.boot</groupId>
294
-
295
- <artifactId>spring-boot-starter-parent</artifactId>
296
-
297
- <version>1.3.5.RELEASE</version>
298
-
299
- <relativePath/> <!-- lookup parent from repository -->
300
-
301
- </parent>
302
-
303
-
304
-
305
- <properties>
306
-
307
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
308
-
309
- <java.version>1.8</java.version>
310
-
311
- </properties>
312
-
313
-
314
-
315
- <dependencies>
316
-
317
- <dependency>
318
-
319
- <groupId>org.springframework.boot</groupId>
320
-
321
- <artifactId>spring-boot-starter-web</artifactId>
322
-
323
- </dependency>
324
-
325
-
326
-
327
- <dependency>
328
-
329
- <groupId>org.springframework.boot</groupId>
330
-
331
- <artifactId>spring-boot-starter-test</artifactId>
332
-
333
- <scope>test</scope>
334
-
335
- </dependency>
336
-
337
- <dependency>
338
-
339
- <groupId>org.springframework.boot</groupId>
340
-
341
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
342
-
343
- </dependency>
344
-
345
- <dependency>
346
-
347
- <groupId>org.springframework.boot</groupId>
348
-
349
- <artifactId>spring-boot-starter-data-jpa</artifactId>
350
-
351
- </dependency>
352
-
353
- <dependency>
354
-
355
- <groupId>org.hsqldb</groupId>
356
-
357
- <artifactId>hsqldb</artifactId>
358
-
359
- <scope>runtime</scope>
360
-
361
- </dependency>
362
-
363
- </dependencies>
364
-
365
-
366
-
367
- <build>
368
-
369
- <plugins>
370
-
371
- <plugin>
372
-
373
- <groupId>org.springframework.boot</groupId>
374
-
375
- <artifactId>spring-boot-maven-plugin</artifactId>
376
-
377
- </plugin>
378
-
379
- </plugins>
380
-
381
- </build>
382
-
383
- </project>
384
-
385
- ```
386
-
387
589
  以上になります。よろしくお願いします。

3

Spring Bootのバージョンの追記

2016/07/07 09:22

投稿

Hiroaki-Yamada
Hiroaki-Yamada

スコア25

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
 
16
16
 
17
- ちなみに、SpringのバージョンはSTS3.7.1で文字コードはutf-8です。
17
+ ちなみに、Spring Bootのバージョンは1.3.6で文字コードはutf-8です。
18
18
 
19
19
  ```java
20
20
 

2

springのバージョンの追加

2016/07/06 09:21

投稿

Hiroaki-Yamada
Hiroaki-Yamada

スコア25

test CHANGED
File without changes
test CHANGED
@@ -12,6 +12,10 @@
12
12
 
13
13
  社内にSpringがわかる人がいないため、どなたかわかる方がいらっしゃいましたらご教授お願いいたします。念のため、コードも載せます。
14
14
 
15
+
16
+
17
+ ちなみに、SpringのバージョンはSTS3.7.1で文字コードはutf-8です。
18
+
15
19
  ```java
16
20
 
17
21
  package com.yamada.springboot;

1

エラー内容の修正

2016/07/06 09:14

投稿

Hiroaki-Yamada
Hiroaki-Yamada

スコア25

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
 
4
4
 
5
- Archive for required library: 'C:/Users/UserName/.m2/repository/org/hibernate/hibernate-core/4.3.11.Final/hibernate-core-4.3.11.Final.jar' in project 'MyBootApp' cannot be read or is not a valid ZIP file
5
+ org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'heloController':
6
+
7
+ Injection of autowired dependencies failed;Error creating bean with name 'myDataRepository':No bean named 'entityManagerFactory' is defined
6
8
 
7
9
 
8
10