質問編集履歴

1

追加

2017/07/05 01:11

投稿

Yoshi--
Yoshi--

score0

test CHANGED
File without changes
test CHANGED
@@ -6,6 +6,10 @@
6
6
 
7
7
 
8
8
 
9
+ import java\.util\.List;
10
+
11
+
12
+
9
13
  import javax\.persistence\.Entity;
10
14
 
11
15
  import javax\.persistence\.FetchType;
@@ -40,6 +44,8 @@
40
44
 
41
45
  @Table\(name = "users"\) //接続するテーブルを指定
42
46
 
47
+ //@ToString\(exclude = "profile"\) //ProfileクラスにUserクラスのフィールドを追加
48
+
43
49
  public class User {
44
50
 
45
51
 
@@ -56,9 +62,11 @@
56
62
 
57
63
  @ManyToOne\(fetch = FetchType\.LAZY\) //Usersとprofileを1対多の関係にするため@OneToManyをつけるcascade = CascadeType\.ALLを設定することでUserの操作をprofileにも伝播できる
58
64
 
65
+ //mappedBy属性に関連先でのプロパティ名を指定
66
+
59
67
  @JoinColumn\(nullable = true, name = "userId"\)
60
68
 
61
- private Profile profile;
69
+ private List<Profile> profile;
62
70
 
63
71
  }
64
72
 
@@ -84,8 +92,6 @@
84
92
 
85
93
  import java\.sql\.Date;
86
94
 
87
- import java\.util\.List;
88
-
89
95
 
90
96
 
91
97
  import javax\.persistence\.CascadeType;
@@ -116,6 +122,8 @@
116
122
 
117
123
  @Data
118
124
 
125
+ //@ToString\(exclude = "user"\)
126
+
119
127
  @NoArgsConstructor //デフォルトコンストラクタも必要な場合
120
128
 
121
129
  @AllArgsConstructor //すべてのフィールドを引数に持つコンストラクタを自動生成
@@ -124,7 +132,7 @@
124
132
 
125
133
  @Table\(name = "profile"\) //接続するテーブルを指定
126
134
 
127
- @ToString\(exclude = "users"\) //Userクラスには対応するProfileクラスのフィールドを追加
135
+ @ToString\(exclude = "user"\)
128
136
 
129
137
  public class Profile {
130
138
 
@@ -150,19 +158,13 @@
150
158
 
151
159
  private byte\[\] image;
152
160
 
153
- @OneToMany\(cascade = CascadeType\.ALL, fetch = FetchType\.LAZY, mappedBy = "profile"\) //mappedBy属性に関連先でのプロパティ名を指定
161
+ @OneToMany\(cascade = CascadeType\.ALL, fetch = FetchType\.LAZY, mappedBy = "profile"\)
154
-
155
-
156
162
 
157
163
  //@ManyToOne\(fetch = FetchType\.LAZY\) //@ManyToOneで多対1の関係にする
158
164
 
159
165
  //@JoinColumn\(nullable = true, name = "userId"\) //外部キーのカラム名の指定
160
166
 
161
- private List<User> users;
167
+ private User user;
162
-
163
-
164
-
165
-
166
168
 
167
169
  }
168
170
 
@@ -190,97 +192,7 @@
190
192
 
191
193
  ```java
192
194
 
193
-
194
-
195
-
196
-
197
- org\.springframework\.beans\.factory\.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource \[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration\.class\]: Invocation of init method failed; nested exception is javax\.persistence\.PersistenceException: \[PersistenceUnit: default\] Unable to build Hibernate SessionFactory
198
-
199
- at org\.springframework\.beans\.factory\.support\.AbstractAutowireCapableBeanFactory\.initializeBean\(AbstractAutowireCapableBeanFactory\.java:1628\) ~\[spring-beans-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
200
-
201
- at org\.springframework\.beans\.factory\.support\.AbstractAutowireCapableBeanFactory\.doCreateBean\(AbstractAutowireCapableBeanFactory\.java:555\) ~\[spring-beans-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
202
-
203
- at org\.springframework\.beans\.factory\.support\.AbstractAutowireCapableBeanFactory\.createBean\(AbstractAutowireCapableBeanFactory\.java:483\) ~\[spring-beans-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
204
-
205
- at org\.springframework\.beans\.factory\.support\.AbstractBeanFactory\$1\.getObject\(AbstractBeanFactory\.java:306\) ~\[spring-beans-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
206
-
207
- at org\.springframework\.beans\.factory\.support\.DefaultSingletonBeanRegistry\.getSingleton\(DefaultSingletonBeanRegistry\.java:230\) ~\[spring-beans-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
208
-
209
- at org\.springframework\.beans\.factory\.support\.AbstractBeanFactory\.doGetBean\(AbstractBeanFactory\.java:302\) ~\[spring-beans-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
210
-
211
- at org\.springframework\.beans\.factory\.support\.AbstractBeanFactory\.getBean\(AbstractBeanFactory\.java:197\) ~\[spring-beans-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
212
-
213
- at org\.springframework\.context\.support\.AbstractApplicationContext\.getBean\(AbstractApplicationContext\.java:1078\) ~\[spring-context-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
214
-
215
- at org\.springframework\.context\.support\.AbstractApplicationContext\.finishBeanFactoryInitialization\(AbstractApplicationContext\.java:857\) ~\[spring-context-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
216
-
217
- at org\.springframework\.context\.support\.AbstractApplicationContext\.refresh\(AbstractApplicationContext\.java:543\) ~\[spring-context-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
218
-
219
- at org\.springframework\.boot\.context\.embedded\.EmbeddedWebApplicationContext\.refresh\(EmbeddedWebApplicationContext\.java:122\) ~\[spring-boot-1\.5\.4\.RELEASE\.jar:1\.5\.4\.RELEASE\]
220
-
221
- at org\.springframework\.boot\.SpringApplication\.refresh\(SpringApplication\.java:693\) \[spring-boot-1\.5\.4\.RELEASE\.jar:1\.5\.4\.RELEASE\]
222
-
223
- at org\.springframework\.boot\.SpringApplication\.refreshContext\(SpringApplication\.java:360\) \[spring-boot-1\.5\.4\.RELEASE\.jar:1\.5\.4\.RELEASE\]
224
-
225
- at org\.springframework\.boot\.SpringApplication\.run\(SpringApplication\.java:303\) \[spring-boot-1\.5\.4\.RELEASE\.jar:1\.5\.4\.RELEASE\]
226
-
227
- at org\.springframework\.boot\.SpringApplication\.run\(SpringApplication\.java:1118\) \[spring-boot-1\.5\.4\.RELEASE\.jar:1\.5\.4\.RELEASE\]
228
-
229
- at org\.springframework\.boot\.SpringApplication\.run\(SpringApplication\.java:1107\) \[spring-boot-1\.5\.4\.RELEASE\.jar:1\.5\.4\.RELEASE\]
230
-
231
- at com\.example\.konkatsu\.KonkatsuApplication\.main\(KonkatsuApplication\.java:10\) \[classes/:na\]
232
-
233
- at sun\.reflect\.NativeMethodAccessorImpl\.invoke0\(Native Method\) ~\[na:1\.8\.0_121\]
234
-
235
- at sun\.reflect\.NativeMethodAccessorImpl\.invoke\(NativeMethodAccessorImpl\.java:62\) ~\[na:1\.8\.0_121\]
236
-
237
- at sun\.reflect\.DelegatingMethodAccessorImpl\.invoke\(DelegatingMethodAccessorImpl\.java:43\) ~\[na:1\.8\.0_121\]
238
-
239
- at java\.lang\.reflect\.Method\.invoke\(Method\.java:498\) ~\[na:1\.8\.0_121\]
240
-
241
- at org\.springframework\.boot\.devtools\.restart\.RestartLauncher\.run\(RestartLauncher\.java:49\) \[spring-boot-devtools-1\.5\.4\.RELEASE\.jar:1\.5\.4\.RELEASE\]
242
-
243
- Caused by: javax\.persistence\.PersistenceException: \[PersistenceUnit: default\] Unable to build Hibernate SessionFactory
244
-
245
- at org\.hibernate\.jpa\.boot\.internal\.EntityManagerFactoryBuilderImpl\.persistenceException\(EntityManagerFactoryBuilderImpl\.java:954\) ~\[hibernate-entitymanager-5\.0\.12\.Final\.jar:5\.0\.12\.Final\]
246
-
247
- at org\.hibernate\.jpa\.boot\.internal\.EntityManagerFactoryBuilderImpl\.build\(EntityManagerFactoryBuilderImpl\.java:882\) ~\[hibernate-entitymanager-5\.0\.12\.Final\.jar:5\.0\.12\.Final\]
248
-
249
- at org\.springframework\.orm\.jpa\.vendor\.SpringHibernateJpaPersistenceProvider\.createContainerEntityManagerFactory\(SpringHibernateJpaPersistenceProvider\.java:60\) ~\[spring-orm-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
250
-
251
- at org\.springframework\.orm\.jpa\.LocalContainerEntityManagerFactoryBean\.createNativeEntityManagerFactory\(LocalContainerEntityManagerFactoryBean\.java:353\) ~\[spring-orm-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
252
-
253
- at org\.springframework\.orm\.jpa\.AbstractEntityManagerFactoryBean\.buildNativeEntityManagerFactory\(AbstractEntityManagerFactoryBean\.java:370\) ~\[spring-orm-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
254
-
255
- at org\.springframework\.orm\.jpa\.AbstractEntityManagerFactoryBean\.afterPropertiesSet\(AbstractEntityManagerFactoryBean\.java:359\) ~\[spring-orm-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
256
-
257
- at org\.springframework\.beans\.factory\.support\.AbstractAutowireCapableBeanFactory\.invokeInitMethods\(AbstractAutowireCapableBeanFactory\.java:1687\) ~\[spring-beans-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
258
-
259
- at org\.springframework\.beans\.factory\.support\.AbstractAutowireCapableBeanFactory\.initializeBean\(AbstractAutowireCapableBeanFactory\.java:1624\) ~\[spring-beans-4\.3\.9\.RELEASE\.jar:4\.3\.9\.RELEASE\]
260
-
261
- \.\.\. 21 common frames omitted
262
-
263
- Caused by: org\.hibernate\.MappingException: Repeated column in mapping for entity: com\.example\.konkatsu\.domain\.User column: user_id \(should be mapped with insert="false" update="false"\)
264
-
265
- at org\.hibernate\.mapping\.PersistentClass\.checkColumnDuplication\(PersistentClass\.java:830\) ~\[hibernate-core-5\.0\.12\.Final\.jar:5\.0\.12\.Final\]
266
-
267
- at org\.hibernate\.mapping\.PersistentClass\.checkPropertyColumnDuplication\(PersistentClass\.java:848\) ~\[hibernate-core-5\.0\.12\.Final\.jar:5\.0\.12\.Final\]
268
-
269
- at org\.hibernate\.mapping\.PersistentClass\.checkColumnDuplication\(PersistentClass\.java:870\) ~\[hibernate-core-5\.0\.12\.Final\.jar:5\.0\.12\.Final\]
270
-
271
- at org\.hibernate\.mapping\.PersistentClass\.validate\(PersistentClass\.java:605\) ~\[hibernate-core-5\.0\.12\.Final\.jar:5\.0\.12\.Final\]
272
-
273
- at org\.hibernate\.mapping\.RootClass\.validate\(RootClass\.java:265\) ~\[hibernate-core-5\.0\.12\.Final\.jar:5\.0\.12\.Final\]
274
-
275
- at org\.hibernate\.boot\.internal\.MetadataImpl\.validate\(MetadataImpl\.java:329\) ~\[hibernate-core-5\.0\.12\.Final\.jar:5\.0\.12\.Final\]
276
-
277
- at org\.hibernate\.boot\.internal\.SessionFactoryBuilderImpl\.build\(SessionFactoryBuilderImpl\.java:443\) ~\[hibernate-core-5\.0\.12\.Final\.jar:5\.0\.12\.Final\]
278
-
279
- at org\.hibernate\.jpa\.boot\.internal\.EntityManagerFactoryBuilderImpl\.build\(EntityManagerFactoryBuilderImpl\.java:879\) ~\[hibernate-entitymanager-5\.0\.12\.Final\.jar:5\.0\.12\.Final\]
280
-
281
- \.\.\. 27 common frames omitted
282
-
283
-
195
+ org\.springframework\.beans\.factory\.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource \[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration\.class\]: Invocation of init method failed; nested exception is org\.hibernate\.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com\.example\.konkatsu\.domain\.Profile\.user
284
196
 
285
197
  ```
286
198