关于Hibernate一对一不能延迟加载的总结

方法是:在主控方的<one-to-one>上加constrained=true,在被控方的class上加lazy=true,其实这个方法是可以的

我们都知道,一对一有两种实现方式,主键关联和外键关联

主键关联:就是说两个表的主键是一样的
                    product(id,name)   image(id,name)
                    其中image的id引用product的id,product是主,image是次
外键关联:就是说是通过一个字段引用另一个表的主键
                    product(id,name)   image(id,name,productid)
                    其中image的productid引用product的id,product是主,image是次

 

在主键关联时,按如下进行实体映射,是可以实现延迟加载的

< hibernate - mapping  package = " onetoonebypk " >  
  
< class  name = " Product1 "  table = " product1 " >  
    
< 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 = " image1 "
                
class = " Image1 "
                cascade
= " all "
                constrained
= " true "
               
>
    
</ one - to - one >

  
</ class >  

</ hibernate - mapping >

 

< hibernate-mapping  package ="onetoonebypk"    >  
  
< class  name ="Image1"  table ="image1"  lazy ="true" >  
    
< id  name ="id" >
      
< generator  class ="foreign" >
        
< param  name ="property" > product1 </ param >
      
</ generator >
    
</ id >
    
< property  name ="name"  column ="name" ></ property >
     
< one-to-one  name ="product1"
                 class
="Product1"
                 cascade
="all" >
    
</ one-to-one >
     
  
</ class >  

</ hibernate-mapping >

当使用外键关联是,如果按如下方式编写映射文件,使不能进行延迟加载的

< hibernate-mapping  package ="onetoonebyfk"    >  
  
< class  name ="Image"  table ="image"  lazy ="true" >  
    
< 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 >

< hibernate-mapping  package ="onetoonebyfk" >  
  
< 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"
                cascade
="all"
                constrained
="true"
               property-ref
="product"
                
>
    
</ one-to-one >
  
</ class >  

</ hibernate-mapping >

为什么呢,我认为是在主方product中,有这样一句话property-ref="product"

这句话的意思是说加外键关联类的属性,如果不指定这个属性,关联类的主键就会被使用

也就是说,如果不制定这个,在查找image时就会使用
select * from image where imageid==product_id 而不是
select * from image where productid==product_id

我们在知道product_id的时候,需要找image中productid等于product_id记录,而不是用image的主键作为比较

去掉propery-def后,倒是可以延迟加载,但在读取product.getImage()方法时会报没有指定id记录的异常,不过这也正常,我们怎么能用image的主键和product_id建立查询条件呢

至于为什么这样,我不是很清楚

总之:我目前的结论是:用主键关联,可以延迟加载,用外键关联,暂时不考虑延迟加载

至于为什么,希望能到大家的帮助

                    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值