1.现在数据库里生成两个表,分别是husband和wife。
2.建立对应的java类。
//husband.java
package com.registation.model;
/**
* Husband entity. @author MyEclipse Persistence Tools
*/
public class Husband implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private Wife wife;
// Constructors
/** default constructor */
public Husband() {
}
/** full constructor */
public Husband(String name, Wife wife) {
this.name = name;
this.wife=wife;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
//wife.java
package com.registation.model;
/**
* Wife entity. @author MyEclipse Persistence Tools
*/
public class Wife implements java.io.Serializable {
// Fields
private Integer id;
private String name;
// Constructors
/** default constructor */
public Wife() {
}
/** full constructor */
public Wife(String name) {
this.name = name;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
//生成相应的Husband.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.registation.model.Husband" table="husband" catalog="hibernate">
<id name="id" type="java.lang.Integer">
<column name="id"></column>
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<!-- name 对应Husband.java里的wife对象。 -->
<many-to-one name="wife" >
<!-- name 对应数据库表里的wife_id字段。 -->
<column name="wife_id"></column>
</many-to-one>
</class>
</hibernate-mapping>
//生成相应的 wife.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.registation.model.Wife" table="wife" catalog="hibernate">
<id name="id" type="java.lang.Integer">
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
</class>
</hibernate-mapping>
用junit3建立测试类Test.java
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import com.registation.model.Husband;
import com.registation.model.Wife;
import junit.framework.TestCase;
public class Test extends TestCase {
public void TestCode(){
Session session=new Configuration().configure().buildSessionFactory().openSession();
session.beginTransaction();
Husband h=new Husband();
h.setName("张三");
Wife w=(Wife) session.get(Wife.class, 1);
h.setWife(w);
session.save(h);
session.beginTransaction().commit();
}
public void addWife(){
Session session=new Configuration().configure().buildSessionFactory().getCurrentSession();
session.beginTransaction();
Wife w=new Wife();
w.setName("李四");
session.save(w);
session.getTransaction().commit();
}
}