Hibernate注解方式实现1-1双向关联
以微博或博客为例,我们希望用户-用户信息设计为如下关系,即用户表用户口令登录等操作、用户信息表用户记录信息:
用户User代码清单:
import ***; /** * @author Barudisshu */ @Entity @Table(name = "t_user", schema = "", catalog = "db_blog") public class User implements Serializable { private int id; //用户自动Id private 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_planetarian") public class Info implements Serializable { private int id; //用户信息自动Id private 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) @PrimaryKeyJoinColumn public User getUser() { return user; } public void setUser(User user) { this.user = user; } //Override Object method }
用户信息表Info使用@PrimaryKeyJoinColumn注解字段user为主键字段。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。