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


Husband.java
package  com.one2one.bean;

import  javax.persistence.CascadeType;
import  javax.persistence.Column;
import  javax.persistence.Entity;
import  javax.persistence.GeneratedValue;
import  javax.persistence.GenerationType;
import  javax.persistence.Id;
import  javax.persistence.OneToOne;
import  javax.persistence.Table;

@Entity
@Table(name
= " husband " ,catalog = " Hibernate_One2One_fk " )
public   class  Husband {
    
    
private   int  id;
    
private  String name;
    
private  Wife wife;
    @Id
    @Column(name
= " id " )
    @GeneratedValue(strategy
= GenerationType.AUTO)
    
public   int  getId() {
        
return  id;
    }
    
public   void  setId( int  id) {
        
this .id  =  id;
    }
    @Column(name
= " name " )
    
public  String getName() {
        
return  name;
    }
    
public   void  setName(String name) {
        
this .name  =  name;
    }
    @OneToOne(cascade
= CascadeType.ALL,mappedBy = " husband " )
    
public  Wife getWife() {
        
return  wife;
    }
    
public   void  setWife(Wife wife) {
        
this .wife  =  wife;
    }
    
}
Wife.java
package  com.one2one.bean;

import  javax.persistence.CascadeType;
import  javax.persistence.Column;
import  javax.persistence.Entity;
import  javax.persistence.GeneratedValue;
import  javax.persistence.GenerationType;
import  javax.persistence.Id;
import  javax.persistence.JoinColumn;
import  javax.persistence.OneToOne;
import  javax.persistence.Table;

@Entity
@Table(name
= " wife " ,catalog = " Hibernate_One2One_fk " )
public   class  Wife {
    
    
private   int  id;
    
private  String name;
    
private  Husband husband;
    @Id
    @Column(name
= " id " )
    @GeneratedValue(strategy
= GenerationType.AUTO)
    
public   int  getId() {
        
return  id;
    }
    
public   void  setId( int  id) {
        
this .id  =  id;
    }
    @Column(name
= " name " )
    
public  String getName() {
        
return  name;
    }
    
public   void  setName(String name) {
        
this .name  =  name;
    }
    @OneToOne(cascade
= CascadeType.ALL)
    @JoinColumn(name
= " husband_id " )
    
public  Husband getHusband() {
        
return  husband;
    }
    
public   void  setHusband(Husband husband) {
        
this .husband  =  husband;
    }
    
}

导出的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 ,
        
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. class 1 );
        System.out.println(husband.getName());
        System.out.println(husband.getWife().getName());
    }

转载于:https://www.cnblogs.com/J2EEPLUS/archive/2011/11/14/2487916.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值