質問編集履歴
1
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,10 +6,90 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
+
import javax.persistence.Entity;
|
10
|
+
|
11
|
+
import javax.persistence.FetchType;
|
12
|
+
|
13
|
+
import javax.persistence.GeneratedValue;
|
14
|
+
|
15
|
+
import javax.persistence.Id;
|
16
|
+
|
17
|
+
import javax.persistence.JoinColumn;
|
18
|
+
|
19
|
+
import javax.persistence.ManyToOne;
|
20
|
+
|
21
|
+
import javax.persistence.Table;
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
import lombok.AllArgsConstructor;
|
26
|
+
|
27
|
+
import lombok.Data;
|
28
|
+
|
29
|
+
import lombok.NoArgsConstructor;
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
@Data //getterメソッド・setterメソッドをコード上に直接書かなくて済む
|
34
|
+
|
35
|
+
@NoArgsConstructor
|
36
|
+
|
37
|
+
@AllArgsConstructor
|
38
|
+
|
39
|
+
@Entity //JPAに管理してもらう
|
40
|
+
|
41
|
+
@Table(name = "users") //接続するテーブルを指定
|
42
|
+
|
43
|
+
public class User {
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
@Id //変数idをプライマリーキーに指定
|
48
|
+
|
49
|
+
@GeneratedValue //自動採番される
|
50
|
+
|
51
|
+
private Integer userId;
|
52
|
+
|
53
|
+
private String mail;
|
54
|
+
|
55
|
+
private String pass;
|
56
|
+
|
57
|
+
@ManyToOne(fetch = FetchType.LAZY) //Usersとprofileを1対多の関係にするため@OneToManyをつけるcascade = CascadeType.ALLを設定することでUserの操作をprofileにも伝播できる
|
58
|
+
|
59
|
+
@JoinColumn(nullable = true, name = "userId")
|
60
|
+
|
61
|
+
private Profile profile;
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
```
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
Profile
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
```java
|
76
|
+
|
77
|
+
//profileテーブルとマッピングさせる
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
package com.example.konkatsu.domain;
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
import java.sql.Date;
|
86
|
+
|
9
87
|
import java.util.List;
|
10
88
|
|
11
89
|
|
12
90
|
|
91
|
+
import javax.persistence.CascadeType;
|
92
|
+
|
13
93
|
import javax.persistence.Entity;
|
14
94
|
|
15
95
|
import javax.persistence.FetchType;
|
@@ -18,9 +98,7 @@
|
|
18
98
|
|
19
99
|
import javax.persistence.Id;
|
20
100
|
|
21
|
-
import javax.persistence.JoinColumn;
|
22
|
-
|
23
|
-
import javax.persistence.Many
|
101
|
+
import javax.persistence.OneToMany;
|
24
102
|
|
25
103
|
import javax.persistence.Table;
|
26
104
|
|
@@ -32,141 +110,175 @@
|
|
32
110
|
|
33
111
|
import lombok.NoArgsConstructor;
|
34
112
|
|
35
|
-
|
36
|
-
|
37
|
-
@Data //getterメソッド・setterメソッドをコード上に直接書かなくて済む
|
38
|
-
|
39
|
-
|
113
|
+
import lombok.ToString;
|
40
|
-
|
114
|
+
|
115
|
+
|
116
|
+
|
41
|
-
@
|
117
|
+
@Data
|
42
|
-
|
118
|
+
|
43
|
-
@
|
119
|
+
@NoArgsConstructor //デフォルトコンストラクタも必要な場合
|
120
|
+
|
44
|
-
|
121
|
+
@AllArgsConstructor //すべてのフィールドを引数に持つコンストラクタを自動生成
|
122
|
+
|
123
|
+
@Entity //エンティティ(一単位として扱われるデータのまとまり)であることを示す。指定したクラス名がテーブルとマッピングされる
|
124
|
+
|
45
|
-
@Table(name = "
|
125
|
+
@Table(name = "profile") //接続するテーブルを指定
|
46
|
-
|
126
|
+
|
47
|
-
|
127
|
+
@ToString(exclude = "users") //Userクラスには対応するProfileクラスのフィールドを追加
|
48
|
-
|
128
|
+
|
49
|
-
public class
|
129
|
+
public class Profile {
|
50
|
-
|
51
|
-
|
52
|
-
|
130
|
+
|
53
|
-
@Id //
|
131
|
+
@Id //主キー
|
54
132
|
|
55
133
|
@GeneratedValue //自動採番される
|
56
134
|
|
57
135
|
private Integer userId;
|
58
136
|
|
137
|
+
private Integer genderId;
|
138
|
+
|
59
|
-
|
139
|
+
private String name;
|
140
|
+
|
60
|
-
|
141
|
+
private Date birthday;
|
142
|
+
|
143
|
+
private Integer height;
|
144
|
+
|
145
|
+
private Integer occupationId;
|
146
|
+
|
147
|
+
private Integer income;
|
148
|
+
|
61
|
-
|
149
|
+
private String text;
|
150
|
+
|
62
|
-
|
151
|
+
private byte[] image;
|
152
|
+
|
153
|
+
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "profile") //mappedBy属性に関連先でのプロパティ名を指定
|
154
|
+
|
155
|
+
|
156
|
+
|
63
|
-
|
157
|
+
//@ManyToOne(fetch = FetchType.LAZY) //@ManyToOneで多対1の関係にする
|
64
|
-
|
65
|
-
|
158
|
+
|
66
|
-
|
67
|
-
|
159
|
+
//@JoinColumn(nullable = true, name = "userId") //外部キーのカラム名の指定
|
68
|
-
|
160
|
+
|
69
|
-
|
161
|
+
private List<User> users;
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
70
166
|
|
71
167
|
}
|
72
168
|
|
169
|
+
|
170
|
+
|
73
171
|
```
|
74
172
|
|
75
173
|
|
76
174
|
|
77
175
|
|
78
176
|
|
177
|
+
![イメージ説明](86e7cd2dc610b97e3ffae54b0fbb253f.png)
|
178
|
+
|
179
|
+
|
180
|
+
|
79
|
-
Profile
|
181
|
+
UsersとProfileを結合させて
|
182
|
+
|
183
|
+
値を表示したいのですが
|
184
|
+
|
185
|
+
次のようなエラー出てしまいます
|
186
|
+
|
187
|
+
|
80
188
|
|
81
189
|
|
82
190
|
|
83
191
|
```java
|
84
192
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
pa
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
import
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
im
|
98
|
-
|
99
|
-
im
|
100
|
-
|
101
|
-
im
|
102
|
-
|
103
|
-
im
|
104
|
-
|
105
|
-
import
|
106
|
-
|
107
|
-
import
|
108
|
-
|
109
|
-
import
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
import
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
p
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
pri
|
144
|
-
|
145
|
-
pri
|
146
|
-
|
147
|
-
pri
|
148
|
-
|
149
|
-
pri
|
150
|
-
|
151
|
-
pri
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
pri
|
158
|
-
|
159
|
-
pri
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
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
|
170
282
|
|
171
283
|
|
172
284
|
|
@@ -176,30 +288,6 @@
|
|
176
288
|
|
177
289
|
|
178
290
|
|
179
|
-
![イメージ説明](86e7cd2dc610b97e3ffae54b0fbb253f.png)
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
UsersとProfileを結合させて
|
184
|
-
|
185
|
-
値を表示したいのですが
|
186
|
-
|
187
|
-
次のようなエラー出てしまいます
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
```java
|
194
|
-
|
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
|
196
|
-
|
197
|
-
```
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
291
|
|
204
292
|
|
205
293
|
どこを直せば良いのでしょうか??
|