场景:一个Student,有联系方式(联系方式名称和联系方式内容),一个student可以拥有多个联系方式,存储时分为两张表 ,一张学生表student,一张联系人表contact,但是只有一个配置文件,也就是一个配置文件生成两张表,这属于一种一对多关系,完全可以使用一对多映射使用两个配置文件配置,但是这里介绍一下使用集合方式组件映射的实现,还是推荐使用前者,因为前者比较直观,容易理解
Contact.java
package com.fgh.hibernate;
/**
* 联系人类
* @author fgh
*
*/
public class Contact {
//联系方式
private String method;
//联系方式内容
private String name;
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Student.java
package com.fgh.hibernate;
import java.util.HashSet;
import java.util.Set;
/**
* 学生类
* @author fgh
*
*/
public class Student {
//因为只使用一个.hbm.xml配置文件 所以这里定义student_component表的外键
private String student_id;
private String id;
private String name;
//定义联系人集合 用于存储多个联系方式
private Set contacts = new HashSet<Contact>();
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;
}
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id) {
this.student_id = student_id;
}
public Set getContacts() {
return contacts;
}
public void setContacts(Set contacts) {
this.contacts = contacts;
}
}
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.fgh.hibernate.Student"
table="student">
<id name="id" column="id" type="string">
<generator class="uuid"></generator>
</id>
<property name="name" column="name" type="string"></property>
<!-- 配置组件映射 只使用一个配置文件 生成两个表-->
<set name="contacts" table="contact">
<key column="student_id"></key>
<composite-element class="com.fgh.hibernate.Contact">
<property name="method" column="method" type="string"></property>
<property name="name" column="name" type="string"></property>
</composite-element>
</set>
</class>
</hibernate-mapping>
建表类:
CreateTable.java
package com.fgh.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class CreateTable {
public static void main(String[] args) {
SchemaExport export = new SchemaExport(new Configuration().configure());
export.create(true, true);
}
}
测试类:HibernateTest.java
package com.fgh.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
* 集合方式的组件映射 跟单表映射有些相似 只是把令一張表中的字段拿到一张表里了 采用組件的方式
*
* @author fgh
*
*/
public class HibernateTest {
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student = new Student();
student.setName("zhangsan");
Contact contact = new Contact();
contact.setMethod("telephone");
contact.setName("1231241234");
Contact contact2 = new Contact();
contact2.setMethod("address");
contact2.setName("beijing");
student.getContacts().add(contact);
student.getContacts().add(contact2);
session.save(student);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (null != tx) {
tx.rollback();
}
} finally {
session.close();
}
}
}
over