HelloWorld 善战者,求之于势,不责于人;故能择人而任势。

知止而后有定,定而后能静,静而后能安,安而后能虑,虑而后能得。物有本末,事有终始。知所先后,则近道矣。

  BlogJava :: 首页 ::  :: 联系 ::  :: 管理 ::
  167 随笔 :: 1 文章 :: 40 评论 :: 0 Trackbacks

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>

posted on 2007-08-14 13:58 helloworld2008 阅读(1634) 评论(1)  编辑  收藏 所属分类: java - hibernatejava

评论

# re: (2) Hibernate 数据关联 one to one(共享主键) 2007-10-23 00:19 helloworld2008
抄自己的书,让别人去说吧  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: