Hibernate一对一外键关联实现方法实例

有连个实体如下:

CREATE   TABLE  `product` (
  `id` 
varchar ( 50 NOT   NULL ,
  `name` 
varchar ( 50 default   NULL ,
  
PRIMARY   KEY   (`id`)
) ENGINE
= InnoDB  DEFAULT  CHARSET = gb2312;

CREATE   TABLE  `product` (
  `id` 
varchar ( 50 NOT   NULL ,
  `name` 
varchar ( 50 default   NULL ,
  
PRIMARY   KEY   (`id`)
) ENGINE
= InnoDB  DEFAULT  CHARSET = gb2312;

 首先,建立这两个实体如下:

 

package  onetoone;

public   class  Image  {
   
private String id;
   
private String name;
   
private Product product;

public Product getProduct() {
    
return product;
}

public void setProduct(Product product) {
    
this.product = product;
}

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;
}

}



package  onetoone;

public   class  Product  {
    
private String id;
    
private String name;
    
private Image image;
    
public String getId() {
        
return id;
    }

    
public void setId(String id) {
        
this.id = id;
    }

    
public Image getImage() {
        
return image;
    }

    
public void setImage(Image image) {
        
this.image = image;
    }

    
public String getName() {
        
return name;
    }

    
public void setName(String name) {
        
this.name = name;
    }


}


接下来建立两个持久类的mapping文件

 

<? 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"
>
<!--  
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping  package ="onetoone" >  
  
< class  name ="Product"  table ="product" >  
    
< id  name ="id"  unsaved-value ="null" >
      
< column  name ="id" ></ column >
      
< generator  class ="uuid.hex" ></ generator >
    
</ id >
    
< property  name ="name"  column ="name" ></ property >
    
< one-to-one  name ="image"
                class
="Image"
                fetch
="join"
                cascade
="all"
                property-ref
="product" >
    
</ one-to-one >
  
</ class >  

</ hibernate-mapping >

<? 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"
>
<!--  
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping  package ="onetoone"    >  
  
< class  name ="Image"  table ="image" >  
    
< id  name ="id" >
      
< generator  class ="uuid.hex" />
    
</ id >
    
< property  name ="name"  column ="name" ></ property >
     
< many-to-one  name ="product"
                 class
="Product"
                 unique
="true"

                 column
="productid" >              
     
</ many-to-one >
  
</ class >  

</ hibernate-mapping >


测试代码:

 

     public   static   void  main(String[] args)  {
        Configuration cfg
=new Configuration();
        cfg.configure();
        SessionFactory sf
=cfg.buildSessionFactory();
        Session session
=sf.openSession();
        Transaction t
=session.beginTransaction();
        
//        Image image=new Image();
//        image.setName("image1");
//        
//        Product product=new Product();
//        product.setName("product1");
//        product.setImage(image);
//        image.setProduct(product);
//        session.save(product);
        

        Product p
=(Product)session.get(Product.class"1");
        System.out.println(p.getImage());

        t.commit();
        System.out.println(
"success");

    }


先在数据库中插入一个product的记录,id为1,image也插入一条记录,其productid为1,便于测试 查询代码

查询结果为
Hibernate: select product0_.id as id1_1_, product0_.name as name1_1_, image1_.id as id0_0_, image1_.name as name0_0_, image1_.productid as productid0_0_ from product product0_ left outer join image image1_ on product0_.id=image1_.productid where product0_.id=?
onetoone.Image@6754d6
success

有一点很重要,也是困扰笔者几个小时的地方,就是在主方(product)的配置文件中<one-to-one>中的
property-ref="product" 如果不加这个属性,Hibernate会使用image表的主键作为关联类的属性,结果HQL语句就会变成
Hibernate: select product0_.id as id1_1_, product0_.name as name1_1_, image1_.id as id0_0_, image1_.name as name0_0_, image1_.productid as productid0_0_ from product product0_ left outer join image image1_ on product0_.id=image1_.id where product0_.id=?

大家注意看,product0_.id=image1_.i,这明显是不对的,应该是product0_.id=image1_.productid ,这样才能正确关联上

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值