日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hibernate注解方式实现1-1双向关联

發布時間:2025/3/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hibernate注解方式实现1-1双向关联 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

以微博或博客為例,我們希望用戶-用戶信息設計為如下關系,即用戶表用戶口令登錄等操作、用戶信息表用戶記錄信息:

用戶User代碼清單:

import ***;/*** @author Barudisshu*/ @Entity @Table(name = "t_user", schema = "", catalog = "db_blog") public class User implements Serializable {private int id; //用戶自動Idprivate String username; //用戶名private String password; //密碼private Info info; //用戶信息@Id@Column(name = "id", nullable = false, insertable = true, updatable = true)public int getId() {return id;}public void setId(int id) {this.id = id;}@Basic@Column(name = "username", nullable = true, insertable = true, updatable = true, length = 255)public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}@Basic@Column(name = "password", nullable = true, insertable = true, updatable = true, length = 255)public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@OneToOne(cascade = CascadeType.ALL, targetEntity = Info.class,mappedBy = "user")public Info getInfo() {return info;}public void setInfo(Info info) {this.info = info;}//Override Object method }

使用@OnetoOne標明1-1關聯關系,并指定映射實體字段為user。

用戶信息Info代碼清單:

import ***;/*** @author Barudisshu*/ @Entity @Table(name = "t_info", schema = "", catalog = "db_blog") public class Info implements Serializable {private int id; //用戶信息自動Idprivate String mood; //發布心情private Integer qq; //QQ號碼private String email; //電子郵箱private String phone; //電話號碼private String avatar; //頭像地址private String qqBlog; //騰訊博客private String sinaBlog; //新浪博客private String leave; //博主留言private User user; //用戶信息@Id@Column(name = "id", nullable = false, insertable = true, updatable = true)public int getId() {return id;}public void setId(int id) {this.id = id;}@Basic@Column(name = "mood", nullable = true, insertable = true, updatable = true, length = 255)public String getMood() {return mood;}public void setMood(String mood) {this.mood = mood;}@Basic@Column(name = "QQ", nullable = true, insertable = true, updatable = true)public Integer getQq() {return qq;}public void setQq(Integer qq) {this.qq = qq;}@Basic@Column(name = "email", nullable = true, insertable = true, updatable = true, length = 400)public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Basic@Column(name = "phone", nullable = true, insertable = true, updatable = true, length = 120)public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Basic@Column(name = "avatar", nullable = true, insertable = true, updatable = true, length = 4000)public String getAvatar() {return avatar;}public void setAvatar(String avatar) {this.avatar = avatar;}@Basic@Column(name = "QQBlog", nullable = true, insertable = true, updatable = true, length = 4000)public String getQqBlog() {return qqBlog;}public void setQqBlog(String qqBlog) {this.qqBlog = qqBlog;}@Basic@Column(name = "SinaBlog", nullable = true, insertable = true, updatable = true, length = 4000)public String getSinaBlog() {return sinaBlog;}public void setSinaBlog(String sinaBlog) {this.sinaBlog = sinaBlog;}@Basic@Column(name = "contact", nullable = true, insertable = true, updatable = true, length = 800)public String getLeave() {return leave;}public void setLeave(String leave) {this.leave = leave;}@OneToOne(optional = false)@PrimaryKeyJoinColumnpublic User getUser() {return user;}public void setUser(User user) {this.user = user;}//Override Object method }

用戶信息表Info使用@PrimaryKeyJoinColumn注解字段user為主鍵字段。

轉載于:https://my.oschina.net/Barudisshu/blog/311893

總結

以上是生活随笔為你收集整理的Hibernate注解方式实现1-1双向关联的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。