登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

清风幻影的博客

Where there is love, I will be there.

 
 
 

日志

 
 
关于我

同是天涯沦落人,相逢何必曾相识. 天生我材必有用,千金散尽还复来. 天若有情天亦老,人间正道是沧桑. 月影西斜人已去, 堤上梅花情依旧, 此情故已成追忆, 美人如玉夜留香

Hibernate一对一外键双向关联(xml配置篇)  

2010-10-21 09:03:12|  分类: hiber_1 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |


Husband.java
package com.one2one.bean;


public class Husband {
    
    
private int id;
    
private String name;
    
private Wife wife;
    
public int getId() {
        
return id;
    }
    
public void setId(int id) {
        
this.id = id;
    }
    
public String getName() {
        
return name;
    }
    
public void setName(String name) {
        
this.name = name;
    }
    
public Wife getWife() {
        
return wife;
    }
    
public void setWife(Wife wife) {
        
this.wife = wife;
    }
    
}
Wife.java
package com.one2one.bean;

public class Wife {
    
    
private int id;
    
private String name;
    
private Husband husband;
    
public int getId() {
        
return id;
    }
    
public void setId(int id) {
        
this.id = id;
    }
    
public String getName() {
        
return name;
    }
    
public void setName(String name) {
        
this.name = name;
    }
    
public Husband getHusband() {
        
return husband;
    }
    
public void setHusband(Husband husband) {
        
this.husband = husband;
    }
    
}
映射文件
Husband.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
    
<hibernate-mapping package="com.one2one.bean">
        
<class name="Husband" table="husband" catalog="Hibernate_One2One_fk">
            
<id name="id" column="id">
                
<generator class="native"/>
            
</id>
            
<property name="name" column="name"/>
            
<one-to-one name="wife" class="Wife" property-ref="husband"/>
        
</class>
    
</hibernate-mapping>
<one-to-one name="wife" class="Wife" property-ref="husband"/> property-ref后的husband为Wife中定义的husband属性
Wife.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
    
<hibernate-mapping package="com.one2one.bean">
        
<class name="Wife" table="wife" catalog="Hibernate_One2One_fk">
            
<id name="id" column="id">
                
<generator class="native"/>
            
</id>
            
<property name="name" column="name"/>
            
<many-to-one name="husband" column="husband_id" unique="true" />
        
</class>
    
</hibernate-mapping>
<many-to-one name="husband" column="husband_id" unique="true" />
many-to-one的作用是在wife表中生成一个外键husband_id,且唯一,并参照husband表中的主键
用hibernate的hbm2ddl导出的sql
create table Hibernate_One2One_fk.husband (
        id 
integer not null auto_increment,
        name 
varchar(255),
        
primary key (id)
    )

    
create table Hibernate_One2One_fk.wife (
        id 
integer not null auto_increment,
        name 
varchar(255),
        husband_id 
integer unique,
        
primary key (id)
    )

    
alter table Hibernate_One2One_fk.wife 
        
add index FK37AF119039AEDD (husband_id), 
        
add constraint FK37AF119039AEDD 
        
foreign key (husband_id) 
        
references Hibernate_One2One_fk.husband (id)

测试示例
@Test
    
public void save(){
        Session session
=HibernateSessionFactory.getSession();
        Transaction transaction
=session.beginTransaction();
        
try {
            transaction.begin();
            Husband husband
=new Husband();
            husband.setName(
"张三");
            session.save(husband);
            Wife wife
=new Wife();
            wife.setName(
"如花");
            wife.setHusband(husband);
            session.save(wife);
            transaction.commit();
        } 
catch (HibernateException e) {
            e.printStackTrace();
        }
    }
    
    @Test
    
public void select(){
        Session session
=HibernateSessionFactory.getSession();
        Husband husband
=(Husband) session.get(Husband.class1);
        System.out.println(husband.getName());
        System.out.println(husband.getWife().getName());
    }
  评论这张
 
阅读(614)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018