有连个实体如下:
`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;
首先,建立这两个实体如下:
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文件
<! 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 >
<! 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 >
测试代码:
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 ,这样才能正确关联上