一对一主键双向关联(用代码控制级联删除)

一、下面的代码可以运行。

1、数据库脚本

--oracle数据库
-- 删除表
DROP TABLE idcard ;
DROP TABLE person ;

-- 创建表
CREATE TABLE person
(
 id VARCHAR2(32) PRIMARY KEY ,
 name VARCHAR2(20) NOT NULL ,
 age NUMBER
) ;
CREATE TABLE idcard
(
 id VARCHAR2(32),
 serial VARCHAR2(18) NOT NULL ,
 expiry NUMBER
) ;

//2############################################

// pojo类

//类一,IdCard

package org.onetoone.com;

public class IdCard {
 private String id;//卡号
 private String serial;//卡的号码(共18位)
 private int expiry;//可以使用年限
 
 private Person person;

 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getSerial() {
  return serial;
 }
 public void setSerial(String serial) {
  this.serial = serial;
 }
 public int getExpiry() {
  return expiry;
 }
 public void setExpiry(int expiry) {
  this.expiry = expiry;
 }
 public Person getPerson() {
  return person;
 }
 public void setPerson(Person person) {
  this.person = person;
 }

}

// pojo类

//类二,Person

package org.onetoone.com;

public class Person {
 private String id ;//perosn的id
 private String name;//名字
 private int  age;   //年龄
 
    private IdCard idcard;
   
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public IdCard getIdcard() {
  return idcard;
 }
 public void setIdcard(IdCard idcard) {
  this.idcard = idcard;
 }
}

2、配置文件。

IdCard.hbm.xml

 

<hibernate-mapping>
 <class name="org.onetoone.com.IdCard" table="idcard">
 <!-- 引用Person的主键作为idCard的主键和外键 -->
  <id name="id" column="id" type="java.lang.String">
   <generator class="foreign">
    <param name="property">person</param>
   </generator>
  </id>
  
     <!-- constrained="true",表示idcard引用了Person主键作外键 -->
  <one-to-one name="person"
   class="org.onetoone.com.Person" 
   constrained="true">
  </one-to-one>


  <property name="serial" type="java.lang.String">
   <column name="serial" length="18" not-null="true" />
  </property>
  <property name="expiry" type="java.lang.Integer">
   <column name="expiry" not-null="true" />
  </property>
 </class>
</hibernate-mapping>

 

 

Person.hbm.xml

 

<hibernate-mapping>
 <class name="org.onetoone.com.Person" table="person">
  <id name="id" type="java.lang.String">
   <column name="id" length="32" />
   <generator class="uuid.hex"></generator>
  </id>
  <property name="name" type="java.lang.String">
   <column name="name" length="20" not-null="true" />
  </property>
  <property name="age" type="java.lang.Integer">
   <column name="age" />
  </property>
  <!-- 下面中的cascade="all",级联保存,删除,修改时 person对象关联的idcard对象。-->
  <!-- Hibernate outer-join参数允许下列三个不同值:
            auto:(默认) 使用外连接抓取关联(对象),如果被关联的对象没有代理(proxy)
            true:一直使用外连接来抓取关联
            false:永远不使用外连接来抓取关联 
         -->
  <one-to-one name="idcard"
   class="org.onetoone.com.IdCard"
   cascade="all"
   outer-join="true"
    >
  </one-to-one>
 </class>
</hibernate-mapping>

3、测试类。PersonIdCardOperation

 

public class PersonIdCardOperation {
 private Session session = null;

 public PersonIdCardOperation() {
  this.session = new Configuration().configure().buildSessionFactory()
    .openSession();
 }

 // 先插入一个用户
 public void insert(Person per) {
  this.session.save(per);
  this.session.beginTransaction().commit();
  session.close();
 }
 //这个查询是用来更新或删除用的
 public Person queryById(String id) {
  Person per = null;
  String hql = "FROM Person AS p where p.id=?";
  Query q = this.session.createQuery(hql);
  q.setString(0, id);
  List all = q.list();
  if (all.size() > 0) {
   per = (Person)all.get(0);
  }
  return per;
 }
   //更新person
 public void update(Person per) {
  this.session.update(per);
  this.session.beginTransaction().commit();
 }

 
 //删除一个person
 public void delete(Person per)
 {  
  this.session.delete(per);
  this.session.beginTransaction().commit() ;
 }

 

 // 一个专门用于更新身份证的操作
 public void update(IdCard ic)
 {
  this.session.update(ic) ;
  this.session.beginTransaction().commit() ;
 }
 
 // 一个专门用于删除身份证的操作
 public void delete(IdCard ic)
 {
  this.session.delete(ic) ;
  this.session.beginTransaction().commit() ;
 }


 /**
  * @param args
  */
 public static void main(String[] args) {
  PersonIdCardOperation po = new PersonIdCardOperation();
//增加  person和idcard  
      /*  Person per =new Person();
  per.setName("jack");
  per.setAge(19851105);

  IdCard ic = new IdCard();
  ic.setSerial("ttttttt");
  ic.setExpiry(44);
  
  per.setIdcard(ic);
  ic.setPerson(per);

  po.insert(per) ;*/                                                                      
//***************************************************
//修改 person与idcard  
      /*  Person uper =po.queryById("ff808081289edac801289edac90e0001");
   
   uper.setName("jack");
   uper.setAge(25);

   IdCard uic = uper.getIdcard();
   uic.setSerial("YYYYYY");
   uic.setExpiry(8);
   
   uper.setIdcard(uic);
   uic.setPerson(uper); 
 
//update它会根据你的信息有没有变化而去更新你的信息,它自己会判断一下的。
      po.update(uper);  */                
  
//***************************************************
//删除  person 与idcard  
       /*   Person dper =po.queryById("ff808081289ed0a101289ed0a22e0001");

    IdCard dic = dper.getIdcard();

    dper.setIdcard(dic);
    dic.setPerson(dper); 
    
              po.delete(dper); */                  
 }

}

 

 

4、总结:这个  一对一主键双向关联(用代码控制级联删除)的 和 在数据库控制级联删除 还是有点不同的。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值