1. 註釋形式的Hibernate多表關聯查詢app
2. 用的是idea這個開發工具(智能糾錯簡直不要太強了)ide
3. 關聯的兩張表的主外鍵字段名稱不一致(一致的話問題就沒有了)工具
我這裏有兩張表,一張是用戶表usernote,一張是用戶筆記表note,搭好Hibernate環境以後,想要關聯查詢兩張表,在實體類上加了註釋開發工具
1 @Data 2 @Entity 3 @Table(name = "note") 4 public class Note implements Serializable {//這是個人筆記表 5 6 @Id 7 @Column(name = "id") 8 @GeneratedValue(generator = "identity") 9 private Integer id; 10 @Column(name = "context") 11 private String Context; 12 @Column(name = "publish_time") 13 private Date publishTime; 14 @Column(name = "user_id")//外鍵字段,關聯用戶表,注意表中字段名稱是user_id 15 private Integer userId; 16 @Column(name = "like_count") 17 private Integer likeCount; 18 19 @ManyToOne(optional = false) //兩者關係是多對一 20 @JoinColumn(name = "user_id", 21 insertable = false,updatable = false,referencedColumnName = "userId") 22 private UserNote userNote; 23 24 } 25 26
1 @Entity 2 @Table(name = "usernote") 3 @Data 4 public class UserNote {//這是個人用戶表 5 6 @Id 7 @Column(name = "userId") 8 @GeneratedValue(generator = "identity") 9 private Integer userId;//個人關聯字段,也是用戶表的主鍵,注意字段名是userId 10 @Column(name = "username") 11 private String username; 12 @Column(name = "address") 13 private String address; 14 @Column(name = "phone") 15 private String phone; 16 17 @OneToMany 18 /************************最開始的錯誤形式******************************/ 19 /**@JoinColumn(name = "userId",referencedColumnName = "user_id")**/ 20 /************************最開始的錯誤形式******************************/ 21 @JoinColumn(name = "user_id",referencedColumnName = "userId") 22 private List<Note> notes; 23 } 24
在idea上(前提是配置了相關的設置),兩個地方都飄紅,最開始還沒在乎就運行了,部署過程直接失敗,結果控制檯報錯idea
Caused by: org.hibernate.MappingException: Unable to find column with logical spa
name: user_id in org.hibernate.mapping.Table(usernote) and its related hibernate
supertables and secondary tablescode
解決的話就是講@JoinColumn裏面的name屬性值改爲外鍵字段就行,爲此還特地去看了一下name屬性的註釋,以下orm
(Optional) The name of the foreign key column. The table in which it is found depends upon the context.
If the join is for a OneToOne or ManyToOne mapping using a foreign key mapping strategy, the foreign key column is in the table of the source entity or embeddable.
If the join is for a unidirectional OneToMany mapping using a foreign key mapping strategy, the foreign key is in the table of the target entity.
If the join is for a ManyToMany mapping or for a OneToOne or bidirectional ManyToOne/OneToMany mapping using a join table, the foreign key is in a join table.
If the join is for an element collection, the foreign key is in a collection table.
Default (only applies if a single join column is used): The concatenation of the following: the name of the referencing relationship property or field of the referencing entity or embeddable class; "_"; the name of the referenced primary key column. If there is no such referencing relationship property or field in the entity, or if the join is for an element collection, the join column name is formed as the concatenation of the following: the name of the entity; "_"; the name of the referenced primary key column.blog
英文水平有限,也就看到這個紅色部分。
老實說idea的源碼下載還有糾錯功能真心不錯