1.学生类:Student.java
package org.yang.hibernate.model;
public class Student
{
private String id;
private String name;
private IdCard idCard;
public Student(){}
public String getId() {
return id;
}
public String getName() {
return name;
}
public IdCard getIdCard() {
return idCard;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
2.身份证类:IdCard.java
package org.yang.hibernate.model;
public class IdCard
{
private String id;
private int number;
private Student student;
public IdCard(){}
public String getId() {
return id;
}
public int getNumber() {
return number;
}
public Student getStudent() {
return student;
}
public void setId(String id) {
this.id = id;
}
public void setNumber(int number) {
this.number = number;
}
public void setStudent(Student student) {
this.student = student;
}
}
3.学生类的映射文件:Student.hbm.xml
<?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" >
<hibernate-mapping>
<class name="org.yang.hibernate.model.Student" table="student">
<id name="id" column="id" type="string">
<generator class="uuid"></generator>
</id>
<property name="name" column="name" type="string"></property>
<one-to-one name="idCard" class="org.yang.hibernate.model.IdCard" cascade="all" fetch="join">
</one-to-one>
</class>
</hibernate-mapping>
4.身份证类的映射文件:IdCard.hbm.xml
<?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" >
<hibernate-mapping>
<class name="org.yang.hibernate.model.IdCard" table="idcard">
<id name="id" column="id">
<generator class="foreign">
<!--
根据当前类IdCard的哪一个属性的名字来生成.
在Student的hbm根据IdCard来生成也是可以的。
这根据个人喜好
-->
<param name="property">student</param>
</generator>
</id>
<property name="number" column="number" type="integer"></property>
<one-to-one name="student" class="org.yang.hibernate.model.Student"></one-to-one>
<!--
外键关联,本质上就是一对多的蜕化形式。在many-to-one元素中增加unique="true"就成了一对一
当修改成这个样子时,也不要忘记了将<id>元素的主键生成方式也改掉。改为:
<id name="id" column="id">
<generator class="uuid" />
</id>
<many-to-one name="student" unique="true" class="org.yang.hibernate.model.Student" column="student_id"/>
-->
</class>
</hibernate-mapping>
5.总配置文件:hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/hibernate</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">yang</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgresqlDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="IdCard.hbm.xml"/>
<mapping resource="Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6.生成数据库表的代码文件:CreateTable.java
package org.yang.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);
}
}
7.测试文件:HibernateTest.java
package org.yang.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.yang.hibernate.model.IdCard; import org.yang.hibernate.model.Student; public class HibernateTest { private static final SessionFactory sessionFactory; static { try { sessionFactory = new Configuration().configure().buildSessionFactory(); } catch(Throwable ex) { System.err.println("Initial SessionFactory Failed " + ex); throw new ExceptionInInitializerError(ex); } } public static void main(String[] args) { Student student = new Student(); student.setName("yangzhiyong"); IdCard idCard = new IdCard(); idCard.setNumber(987654); student.setIdCard(idCard); idCard.setStudent(student); Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(student); tx.commit(); } catch (Exception e) { if (null != tx) { tx.rollback(); } e.printStackTrace(); } finally { session.close(); } } }