前提・実現したいこと
Java SpringFrameWork で MySQLのテーブルを1対1で連結させたい。
Category クラスの code 列と Noteクラスの category_code 列を連結させたい。
どなたかわかりませんか?
#db
+--------------------+
| Tables_in_notebook |
+--------------------+
| categories |
| notes |
+--------------------+
notes table
+---------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
|** category_code ** | varchar(10) | YES | | NULL | |
| title | varchar(20) | YES | | NULL | |
| time_stamp | varchar(20) | YES | | NULL | |
| body | text | YES | | NULL | |
+---------------+-------------+------+-----+---------+----------------+
categories table
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| code | varchar(10) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
###参考にしたページ
JPAにおける一対一の関係
@JoinColumn
発生している問題・エラーメッセージ
テーブルを関連付けしようとすると以下のメッセージが出る。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to find column with logical name: category_code in org.hibernate.mapping.Table(categories) and its related supertables and secondary tables
該当のソースコード
Category.java
java
1package com.kenta.tabuchi; 2 3import javax.persistence.Column; 4import javax.persistence.Entity; 5import javax.persistence.Id; 6import javax.persistence.OneToOne; 7import javax.persistence.Table; 8 9@Entity 10@Table(name="categories") 11public class Category { 12 @OneToOne(mappedBy = "categories") 13 private Note note; 14 15 @Id 16 @Column 17 protected String code; 18 @Column 19 protected String name; 20 21 public String getCode() { 22 return code; 23 } 24 public void setCode(String code) { 25 this.code = code; 26 } 27 public String getName() { 28 return name; 29 } 30 public void setName(String name) { 31 this.name = name; 32 } 33}
Note.java
java
1package com.kenta.tabuchi; 2 3import javax.persistence.CascadeType; 4import javax.persistence.Column; 5import javax.persistence.Entity; 6import javax.persistence.GeneratedValue; 7import javax.persistence.GenerationType; 8import javax.persistence.Id; 9import javax.persistence.JoinColumn; 10import javax.persistence.OneToOne; 11import javax.persistence.Table; 12 13@Entity 14@Table(name="notes") 15public class Note { 16 17 @OneToOne 18 @JoinColumn(nullable = true, name = "code",referencedColumnName = "category_code") 19 private Category category; 20 public Note() { 21 super(); 22 category = new Category(); 23 } 24 25 @Id 26 @GeneratedValue(strategy = GenerationType.AUTO) 27 @Column 28 protected Long id; 29 30 @Column 31 protected String category_code; 32 33 @Column 34 protected String time_stamp; 35 36 @Column 37 protected String title; 38 39 @Column 40 protected String body; 41 42 public Long getId() { 43 return id; 44 } 45 public void setId(Long id) { 46 this.id = id; 47 } 48 public String getCategory_code() { 49 return category_code; 50 } 51 public void setCategory_code(String category_code) { 52 this.category_code = category_code; 53 } 54 public String getTitle() { 55 return title; 56 } 57 public void setTitle(String title) { 58 this.title = title; 59 } 60 public String getTime_stamp() { 61 return time_stamp; 62 } 63 public void setTime_stamp(String time_stamp) { 64 this.time_stamp = time_stamp; 65 } 66 public String getBody() { 67 return body; 68 } 69 public void setBody(String body) { 70 this.body = body; 71 } 72} 73
試したこと
変数名を入れ替えたり、JoinColumnの引数を変えたりしましたが、変わらず。
それぞれのテーブルは単独では正しく動作しており、HTMLへの出力を確認しております。
補足情報(FW/ツールのバージョンなど)
Mac0S
StS
MySQL
回答1件
あなたの回答
tips
プレビュー