站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题Eclipse快速上手Hibernate--5. 关联映射(一对一)

Eclipse快速上手Hibernate--5. 关联映射(一对一)

摘要: Eclipse快速上手Hibernate--5. 关联映射(一对一)
Hibernate中的关联(Association)映射主要有三种:一对一关联,一对多(或多对一)关联,多对多关联。每种关联都可以分为单向和双向两种。 这篇文章主要说的是在Hibernate中的一对一关联,可以参考Hibernate官方文档的第5章至于环境设置,可以参考这个系列的前面几篇文章。 一对一关联有两种映射方式:一种是使用主键关联,限制两个数据表的主键使用相同的值;另一种是一个外键和一个惟一关键字对应。 这里用关联映射将上篇文章《组件映射》改写了一下,一个人对应一个地址,这是一个单向关联,先谈谈如何使用主键关联。
1. 创建项目 · 新建一个Java项目:AssociationMapping,注意选中“创建单独的源文件夹和输出文件夹”,同时添加“用户库”:hibernate。
2. 编写类文件 · 新建一个类,包名:javamxj.hibernate.association.one2one,类名:Person。

Person.java

/*

 * Hibernate - 关联(Association)映射

 * 创建日期 2005-4-25

 * @author javamxj(分享java快乐)

 * @link  Blog: htpp://blog.csdn.net/javamxj/

 *              htpp://javamxj.mblogger.cn

 */

package javamxj.hibernate.association.one2one;

/**

 * @hibernate.class table = "T_Person"

 */

public class Person {

private Long id;

private String username;

private Address address;

/**

 * @hibernate.id

 *   generator-class="foreign"

 * @hibernate.generator-param

 * name="property"

 * value="address"

 */

public Long getId() {return id;}

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

/**

 * @hibernate.property

 * length="15"

 * not-null="true"

 */

public String getUsername() {return username;}

public void setUsername(String username) {this.username = username;}

/**

 * @hibernate.one-to-one

 * cascade="all"

 */

public Address getAddress() {return address;}

public void setAddress(Address address) {this.address = address;}

}

· 注意Person类的主键生成方式是“foreign”,使其参考Address类的主键。· 在最后几行中指明了如何引用Address类的方式,其中cascade="all" 表示所有情况下均进行级联操作。 · 这里将Address类独立映射为一个表。

Address.java

package javamxj.hibernate.association.one2one;

/**

 * @hibernate.class table = "T_Address"

 */

public class Address {

private String country;

private String city;

private String street;

private String zipCode;

private Long id;

public Address() {}

public Address(String country, String city, String street, String zipcode) {

super();

this.country = country;

this.city = city;

this.street = street;

this.zipCode = zipcode;

}

/**

 * @hibernate.id

 *generator-class="hilo"

 *unsaved-value="null"

 */

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

/**

 * @hibernate.property

 *  length = "12"

 */

public String getCity() {return city;}

public void setCity(String city) {this.city = city;}

/**

 * @hibernate.property

 *  length = "12"

 */

public String getCountry() {return country;}

public void setCountry(String country) {this.country = country;}

/**

 * @hibernate.property

 *  length = "6"

 */

public String getZipCode() {return zipCode;}

public void setZipCode(String number) {this.zipCode = number;}

/**

 * @hibernate.property

 *  length = "12"

 */

public String getStreet() {return street;}

public void setStreet(String street) {this.street = street;}

}
3. 运行任务
· 复制《Eclipse快速上手Hibernate--4. 继承映射(1)》文中的build.xml到项目根目录下。
· 双击“generate-hbm”任务,会发现在包中产生了Person.hbm.xml和Address.hbm.xml文件,在src目录下会多了一个hibernate.cfg.xml文件,如果没有,按F5键刷新一下。 · 看看Person类的映射文件中的重点部分:

address
import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.Transaction; import net.sf.hibernate.cfg.Configuration; public class Demo { public static void main(String[] args) throws HibernateException { Address address1 = new Address("中国", "上海", "普陀", "200055"); Person p1 = new Person(); p1.setUsername("JavaMXJ"); p1.setAddress(address1); Address address2 = new Address("中国", "北京", "海淀", "100086"); Person p2 = new Person(); p2.setUsername("张三"); p2.setAddress(address2); SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session sess = sf.openSession(); Transaction tx= sess.beginTransaction(); sess.save(p1); sess.save(p2); tx.commit(); sess.close(); sf.close(); } }
· 运行这个类,数据表中生成如下数据: 注意这两个表中的主键值相同的。 · 最后的项目结构如下:
5. 另一种映射方式 这里简单说说一个外键和一个惟一关键字对应的方式。 以上面的文章为基础,只要改动一下Person.java即可。

Person.java

/*

 * Hibernate - 关联(Association)映射

 * 创建日期 2005-4-25

 * @author javamxj(分享java快乐)

 * @link  Blog: htpp://blog.csdn.net/javamxj/

 *              htpp://javamxj.mblogger.cn

 */

package javamxj.hibernate.association.one2one;

/**

 * @hibernate.class table = "T_Person"

 */

public class Person {

private Long id;

private String username;

private Address address;

/**

 * @hibernate.id

 *generator-class="hilo"

 *unsaved-value="null"

 */

public Long getId() {return id;}

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

/**

 * @hibernate.property

 * length="15"

 * not-null="true"

 */

public String getUsername() {return username;}

public void setUsername(String username) {this.username = username;}

/**

 * @hibernate.many-to-one

 *  column = "fk_Address"

 *  cascade="all"

 *  unique="true"

 */

public Address getAddress() {return address;}

public void setAddress(Address address) {this.address = address;}

}

· 需要改动的部分用斜体字标出来了,有两处,一处是定义主键的生成方式,一处是Person类如何引用Address类。 · 看看Person类的映射文件中的相关部分: