CREATE TABLE t_users
(
id int4 NOT NULL DEFAULT nextval('t_user_id_seq'::text),
age int4,
name varchar(30),
CONSTRAINT t_users_pkey PRIMARY KEY (id)
)
CREATE TABLE t_passports
(
id int4 NOT NULL DEFAULT nextval('t_user_id_seq'::text),
serial varchar(30),
expiry int4,
CONSTRAINT t_passports_pkey PRIMARY KEY (id),
CONSTRAINT t_passports_id_fkey FOREIGN KEY (id)
REFERENCES t_users (id) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT
)
第一部分 使用xml配置
源代码下载http://www.blogjava.net/Files/zhaochengming/one2one%20xmlcomfig%20hibernate.rar
t_users和t_passports根据t_user.id一对一关联
TUsers.hbm.xml
<hibernate-mapping>
<class name="com.tables.one2one.TUsers" table="t_users" schema="public">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="sequence">
<param name="sequence">t_user_id_seq</param>
</generator>
</id>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
<property name="name" type="java.lang.String">
<column name="name" length="30" />
</property>
<!-- 如果cascade=none的话,就不会级联更新了,就是说只在t_users中插入值,在t_passports中不插入值 -->
<one-to-one name="passport" class="com.tables.one2one.TPassports"
cascade="all" outer-join="true">
</one-to-one>
</class>
</hibernate-mapping>
TPassports.hbm.xml
<hibernate-mapping>
<class name="com.tables.one2one.TPassports" table="t_passports" schema="public">
<id name="id" type="java.lang.Integer">
<column name="id" />
<!-- 这里一定要改成foreign -->
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<one-to-one name="user" class="com.tables.one2one.TUsers" constrained="true"></one-to-one>
<property name="serial" type="java.lang.String">
<column name="serial" length="30" />
</property>
<property name="expiry" type="java.lang.Integer">
<column name="expiry" />
</property>
</class>
</hibernate-mapping>
测试
public class One2OneTest extends BaseHibernateDAO {
public void test() {
TUsers user = new TUsers();
user.setAge(100);
user.setName("CEYLIN");
TPassports passport = new TPassports();
passport.setSerial("PCN1002211");
passport.setExpiry(20080806);
passport.setUser(user);
user.setPassport(passport);
Session session = this.getSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
}
}
第二部分 使用注解,
源代码下载 http://www.blogjava.net/Files/zhaochengming/one2oneAnno1.rar
package com;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="t_users")
public class TUser {
@Id
@Column(name = "user_id")
@GeneratedValue(generator = "sequence")
@SequenceGenerator(name = "sequence", sequenceName = "t_table_id_seq")
private Integer id;
@Column(name = "age")
private Integer age;
@Column(name= "name")
private String name;
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private TPassport passport;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
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 TPassport getPassport() {
return passport;
}
public void setPassport(TPassport passport) {
this.passport = passport;
}
}
package com;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
@Entity
@Table(name = "t_passports")
public class TPassport {
@Id
@GeneratedValue(generator = "fk")
@GenericGenerator(strategy = "foreign", name = "fk", parameters = @Parameter(name="property", value="user"))
private Integer id;
@Column(name = "serial")
private String serial;
@Column(name="expiry")
private Integer expiry;
@OneToOne(mappedBy="passport", optional = false)
private TUser user;
public Integer getExpiry() {
return expiry;
}
public void setExpiry(Integer expiry) {
this.expiry = expiry;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSerial() {
return serial;
}
public void setSerial(String serial) {
this.serial = serial;
}
public TUser getUser() {
return user;
}
public void setUser(TUser user) {
this.user = user;
}
}
</script>