Hibernate注解方式实现1-1双向关联
生活随笔
收集整理的這篇文章主要介紹了
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双向关联的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: openldap 中文乱码问题
- 下一篇: [复变函数]第19堂课 5.3 解析函数