質問するログイン新規登録

質問編集履歴

5

エラー内容の変更

2016/07/08 01:04

投稿

Hiroaki-Yamada
Hiroaki-Yamada

スコア25

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  Ecripseを使ってSpring Bootプログラミング入門という参考書をすすめているのですが、
2
2
 
3
- java.lang.IllegalArgumentException: Sources must not be empty
3
+ 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
4
4
 
5
5
  というエラーが出て困っています。
6
6
  社内にSpringがわかる人がいないため、どなたかわかる方がいらっしゃいましたらご教授お願いいたします。念のため、コードも載せます。

4

コードの修正

2016/07/08 01:04

投稿

Hiroaki-Yamada
Hiroaki-Yamada

スコア25

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,6 @@
1
1
  Ecripseを使ってSpring Bootプログラミング入門という参考書をすすめているのですが、
2
2
 
3
- org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'heloController':
3
+ java.lang.IllegalArgumentException: Sources must not be empty
4
- Injection of autowired dependencies failed;Error creating bean with name 'myDataRepository':No bean named 'entityManagerFactory' is defined
5
4
 
6
5
  というエラーが出て困っています。
7
6
  社内にSpringがわかる人がいないため、どなたかわかる方がいらっしゃいましたらご教授お願いいたします。念のため、コードも載せます。
@@ -16,118 +15,214 @@
16
15
  import javax.persistence.GenerationType;
17
16
  import javax.persistence.Id;
18
17
  import javax.persistence.Table;
18
+ import javax.validation.constraints.Max;
19
+ import javax.validation.constraints.Min;
20
+ import javax.validation.constraints.NotNull;
19
21
 
22
+ import org.hibernate.validator.constraints.Email;
23
+ import org.hibernate.validator.constraints.NotEmpty;
24
+
20
25
  @Entity
21
26
  @Table(name="mydata")
27
+ public class MyData {
22
28
 
23
- public class MyData {
24
29
  @Id
25
30
  @GeneratedValue(strategy = GenerationType.AUTO)
26
31
  @Column
32
+ @NotNull
27
33
  private long id;
28
34
 
29
35
  @Column(length = 50, nullable = false)
36
+ @NotEmpty
30
37
  private String name;
31
38
 
32
39
  @Column(length = 200, nullable = true)
40
+ @Email
33
41
  private String mail;
34
42
 
35
43
  @Column(nullable = true)
44
+ @Min(0)
45
+ @Max(200)
36
46
  private Integer age;
37
47
 
38
48
  @Column(nullable = true)
39
49
  private String memo;
40
50
 
41
- public long getId(){
51
+ public long getId() {
42
52
  return id;
43
53
  }
44
-
45
- public void setId(long id){
54
+ public void setId(long id) {
46
- this.id =id;
55
+ this.id = id;
47
56
  }
48
57
 
49
- public String getName(){
58
+ public String getName() {
50
59
  return name;
51
60
  }
52
-
53
- public void setName(String name){
61
+ public void setName(String name) {
54
62
  this.name = name;
55
63
  }
56
64
 
57
- public String getMail(){
65
+ public String getMail() {
58
66
  return mail;
59
67
  }
60
-
61
- public void setMail(String mail){
68
+ public void setMail(String mail) {
62
69
  this.mail = mail;
63
70
  }
64
71
 
65
- public Integer getAge(){
72
+ public Integer getAge() {
66
73
  return age;
67
74
  }
68
-
69
- public void setAge(Integer age){
75
+ public void setAge(Integer age) {
70
76
  this.age = age;
71
77
  }
72
78
 
73
- public String getMemo(){
79
+ public String getMemo() {
74
80
  return memo;
75
81
  }
76
-
77
- public void setMemo(String memo){
82
+ public void setMemo(String memo) {
78
83
  this.memo = memo;
79
84
  }
80
85
  }
81
-
82
86
  ```
83
87
  ```java
84
88
  package com.yamada.springboot.repositories;
85
89
 
86
- import com.yamada.springboot.MyData;
90
+ import java.util.List;
91
+
87
92
  import org.springframework.data.jpa.repository.JpaRepository;
88
93
  import org.springframework.stereotype.Repository;
89
94
 
95
+ import com.yamada.springboot.MyData;
96
+
90
97
  @Repository
91
- public interface MyDataRepository extends JpaRepository<MyData, Long>{}
98
+ public interface MyDataRepository extends JpaRepository<MyData, Long> {
99
+
100
+ public MyData findById(Long name);
101
+ public List<MyData> findByNameLike(String name);
102
+ public List<MyData> findByIdIsNotNullOrderByIdDesc();
103
+ public List<MyData> findByAgeGreaterThan(Integer age);
104
+ public List<MyData> findByAgeBetween(Integer age1, Integer age2);
105
+
106
+ }
107
+
92
108
  ```
93
109
  ```java
94
110
  package com.yamada.springboot;
95
111
 
112
+ import javax.annotation.PostConstruct;
113
+
96
114
  import org.springframework.beans.factory.annotation.Autowired;
97
115
  import org.springframework.stereotype.Controller;
116
+ import org.springframework.transaction.annotation.Transactional;
117
+ import org.springframework.validation.BindingResult;
118
+ import org.springframework.validation.annotation.Validated;
119
+ import org.springframework.web.bind.annotation.ModelAttribute;
120
+ import org.springframework.web.bind.annotation.PathVariable;
98
121
  import org.springframework.web.bind.annotation.RequestMapping;
122
+ import org.springframework.web.bind.annotation.RequestMethod;
123
+ import org.springframework.web.bind.annotation.RequestParam;
99
124
  import org.springframework.web.servlet.ModelAndView;
125
+
100
126
  import com.yamada.springboot.repositories.MyDataRepository;
101
127
 
102
128
  @Controller
103
- public class HeloController {
129
+ public class HelloController {
104
130
 
105
131
  @Autowired
106
132
  MyDataRepository repository;
107
133
 
108
- @RequestMapping("/")
134
+ @RequestMapping(value = "/", method = RequestMethod.GET)
109
- public ModelAndView index(ModelAndView mav){
135
+ public ModelAndView index(
136
+ @ModelAttribute("formModel") MyData mydata,
137
+ ModelAndView mav) {
110
138
  mav.setViewName("index");
111
- mav.addObject("msg", "this is sample content.");
139
+ mav.addObject("msg","this is sample content.");
140
+ mav.addObject("formModel",mydata);
112
141
  Iterable<MyData> list = repository.findAll();
113
- mav.addObject("data", list);
142
+ mav.addObject("datalist",list);
114
143
  return mav;
115
144
  }
116
- }
117
- ```
118
- ```java
119
- package com.yamada.springboot;
120
145
 
146
+ @RequestMapping(value = "/", method = RequestMethod.POST)
147
+ @Transactional(readOnly=false)
148
+ public ModelAndView form(
149
+ @ModelAttribute("formModel")
150
+ @Validated MyData mydata,
151
+ BindingResult result,
152
+ ModelAndView mov) {
153
+ ModelAndView res = null;
154
+ if (!result.hasErrors()){
155
+ repository.saveAndFlush(mydata);
156
+ res = new ModelAndView("redirect:/");
157
+ } else {
121
- import org.springframework.boot.SpringApplication;
158
+ mov.setViewName("index");
122
- import org.springframework.boot.autoconfigure.SpringBootApplication;
159
+ mov.addObject("msg","sorry, error is occured...");
160
+ Iterable<MyData> list = repository.findAll();
161
+ mov.addObject("datalist",list);
162
+ res = mov;
163
+ }
164
+ return res;
165
+ }
123
166
 
167
+ @PostConstruct
124
- @SpringBootApplication
168
+ public void init(){
169
+ MyData d1 = new MyData();
170
+ d1.setName("tuyano");
171
+ d1.setAge(123);
172
+ d1.setMail("syoda@tuyano.com");
173
+ d1.setMemo("this is my data!");
174
+ repository.saveAndFlush(d1);
175
+ MyData d2 = new MyData();
176
+ d2.setName("hanako");
177
+ d2.setAge(15);
178
+ d2.setMail("hanako@flower");
179
+ d2.setMemo("my girl friend.");
180
+ repository.saveAndFlush(d2);
181
+ MyData d3 = new MyData();
125
- public class MyBootAppApplication {
182
+ d3.setName("sachiko");
183
+ d3.setAge(37);
184
+ d3.setMail("sachico@happy");
185
+ d3.setMemo("my work friend...");
186
+ repository.saveAndFlush(d3);
187
+ }
126
188
 
189
+ @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
127
- public static void main(String[] args) {
190
+ public ModelAndView edit(@ModelAttribute MyData mydata,
191
+ @PathVariable int id,ModelAndView mav) {
192
+ mav.setViewName("edit");
128
- SpringApplication.run(MyBootAppApplication.class, args);
193
+ mav.addObject("title","edit mydata.");
194
+ MyData data = repository.findById((long)id);
195
+ mav.addObject("formModel",data);
196
+ return mav;
129
197
  }
198
+
199
+ @RequestMapping(value = "/edit", method = RequestMethod.POST)
200
+ @Transactional(readOnly=false)
201
+ public ModelAndView update(@ModelAttribute MyData mydata,
202
+ ModelAndView mav) {
203
+ repository.saveAndFlush(mydata);
204
+ return new ModelAndView("redirect:/");
205
+ }
206
+
207
+ @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
208
+ public ModelAndView delete(@PathVariable int id,
209
+ ModelAndView mav) {
210
+ mav.setViewName("delete");
211
+ mav.addObject("title","delete mydata.");
212
+ MyData data = repository.findById((long)id);
213
+ mav.addObject("formModel",data);
214
+ return mav;
215
+ }
216
+
217
+ @RequestMapping(value = "/delete", method = RequestMethod.POST)
218
+ @Transactional(readOnly=false)
219
+ public ModelAndView remove(@RequestParam long id,
220
+ ModelAndView mav) {
221
+ repository.delete(id);
222
+ return new ModelAndView("redirect:/");
223
+ }
130
224
  }
225
+
131
226
  ```
132
227
  ```xml
133
228
  <?xml version="1.0" encoding="UTF-8"?>
@@ -141,18 +236,19 @@
141
236
  <packaging>jar</packaging>
142
237
 
143
238
  <name>MyBootApp</name>
144
- <description>sample project for Spring Boot</description>
239
+ <description>Sample project for Spring Boot</description>
145
240
 
146
241
  <parent>
147
242
  <groupId>org.springframework.boot</groupId>
148
243
  <artifactId>spring-boot-starter-parent</artifactId>
149
- <version>1.3.5.RELEASE</version>
244
+ <version>1.3.6.RELEASE</version>
150
245
  <relativePath/> <!-- lookup parent from repository -->
151
246
  </parent>
152
247
 
153
248
  <properties>
154
249
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
155
250
  <java.version>1.8</java.version>
251
+ <start-class>org.springframework.boot.SpringApplication</start-class>
156
252
  </properties>
157
253
 
158
254
  <dependencies>
@@ -171,14 +267,18 @@
171
267
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
172
268
  </dependency>
173
269
  <dependency>
174
- <groupId>org.springframework.boot</groupId>
270
+ <groupId>org.springframework.boot</groupId>
175
- <artifactId>spring-boot-starter-data-jpa</artifactId>
271
+ <artifactId>spring-boot-starter-data-jpa</artifactId>
176
- </dependency>
272
+ </dependency>
177
273
  <dependency>
178
- <groupId>org.hsqldb</groupId>
274
+ <groupId>org.hsqldb</groupId>
179
275
  <artifactId>hsqldb</artifactId>
180
276
  <scope>runtime</scope>
181
277
  </dependency>
278
+ <dependency>
279
+ <groupId>org.springframework.boot</groupId>
280
+ <artifactId>spring-boot-starter-data-mongodb</artifactId>
281
+ </dependency>
182
282
  </dependencies>
183
283
 
184
284
  <build>
@@ -189,6 +289,7 @@
189
289
  </plugin>
190
290
  </plugins>
191
291
  </build>
292
+
192
293
  </project>
193
294
  ```
194
295
  以上になります。よろしくお願いします。

3

Spring Bootのバージョンの追記

2016/07/07 09:22

投稿

Hiroaki-Yamada
Hiroaki-Yamada

スコア25

title CHANGED
File without changes
body CHANGED
@@ -6,7 +6,7 @@
6
6
  というエラーが出て困っています。
7
7
  社内にSpringがわかる人がいないため、どなたかわかる方がいらっしゃいましたらご教授お願いいたします。念のため、コードも載せます。
8
8
 
9
- ちなみに、SpringのバージョンはSTS3.7.1で文字コードはutf-8です。
9
+ ちなみに、Spring Bootのバージョンは1.3.6で文字コードはutf-8です。
10
10
  ```java
11
11
  package com.yamada.springboot;
12
12
 

2

springのバージョンの追加

2016/07/06 09:21

投稿

Hiroaki-Yamada
Hiroaki-Yamada

スコア25

title CHANGED
File without changes
body CHANGED
@@ -5,6 +5,8 @@
5
5
 
6
6
  というエラーが出て困っています。
7
7
  社内にSpringがわかる人がいないため、どなたかわかる方がいらっしゃいましたらご教授お願いいたします。念のため、コードも載せます。
8
+
9
+ ちなみに、SpringのバージョンはSTS3.7.1で文字コードはutf-8です。
8
10
  ```java
9
11
  package com.yamada.springboot;
10
12
 

1

エラー内容の修正

2016/07/06 09:14

投稿

Hiroaki-Yamada
Hiroaki-Yamada

スコア25

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,7 @@
1
1
  Ecripseを使ってSpring Bootプログラミング入門という参考書をすすめているのですが、
2
2
 
3
- 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
3
+ org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'heloController':
4
+ Injection of autowired dependencies failed;Error creating bean with name 'myDataRepository':No bean named 'entityManagerFactory' is defined
4
5
 
5
6
  というエラーが出て困っています。
6
7
  社内にSpringがわかる人がいないため、どなたかわかる方がいらっしゃいましたらご教授お願いいたします。念のため、コードも載せます。