1. Hibernate Annotation注解OneToOne
原理就不介绍了
2. 数据字典表
tb_addressInfo
CREATE TABLE `tb_addressinfo` ( `id` int(11) NOT NULL AUTO_INCREMENT, `detail` varchar(255) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `userInfo_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`), UNIQUE KEY `userInfo_id` (`userInfo_id`), KEY `FK2F779271FC1A0B7B` (`userInfo_id`), CONSTRAINT `FK2F779271FC1A0B7B` FOREIGN KEY (`userInfo_id`) REFERENCES `tb_userinfo` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
tb_userinfo
CREATE TABLE `tb_userinfo` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
AddressInfo
package org.zk.liuqing.domain; import static javax.persistence.GenerationType.IDENTITY; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; /** * * @author LiuQing * @see 显示地址 * @version 1.0 * @date 2010-6-26 下午11:00:02 */ @Entity(name="AddressInfo") @Table(name="tb_addressInfo") public class AddressInfo implements Serializable { private static final long serialVersionUID = -3387012027037438359L; @Id @GeneratedValue(strategy=IDENTITY) @Column(name="id", unique=true, nullable=false) private Integer id; @Column private String name; @Column private String detail; @OneToOne @JoinColumn(name="userInfo_id",insertable=true,unique=true) private UserInfo userInfo; public UserInfo getUserInfo() { return userInfo; } public void setUserInfoAddressInfo(UserInfo userInfo) { this.userInfo = userInfo; this.userInfo.setAddressInfo(this); } public void setUserInfo(UserInfo userInfo) { this.userInfo = userInfo; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } }
package org.zk.liuqing.domain; import static javax.persistence.GenerationType.IDENTITY; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; /** * * @author LiuQing * @see 用户信息 * @version 1.0 * @date 2010-6-26 下午11:00:45 */ @Entity(name="UserInfo") @Table(name="tb_userInfo") public class UserInfo implements Serializable { private static final long serialVersionUID = 1054325973011267306L; @Id @GeneratedValue(strategy=IDENTITY) @Column(name="id", unique=true, nullable=false) private Integer id; @Column private String name; @OneToOne(mappedBy="userInfo",fetch=FetchType.EAGER) private AddressInfo addressInfo; public AddressInfo getAddressInfo() { return addressInfo; } public void setAddressInfoUserInfo(AddressInfo addressInfo) { this.addressInfo = addressInfo; this.addressInfo.setUserInfo(this); } public void setAddressInfo(AddressInfo addressInfo) { this.addressInfo = addressInfo; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }