http://www.blogjava.net/Files/1504/Hibernate-onetomany.txt一个一对多的完整例子:资料来源网络,进行整理.
people表:{peopleId,name}
address表:{addressId,peopleId,addressName,codeNumber}
package com.gpdi.test;
import org.hibernate.Session;
import com.gpdi.orm.object.Address;
import com.gpdi.orm.object.People;
public class Testp {
/**
* @param args
*/
public static void main(String[] args)
{
People people = new People();
people.setName("linda");
Address address = new Address();
address.setAddressName("yunnan");
address.setCodeNumber("564123");
address.setPeople(people);
people.getAddresses().add(address);
Session session = HibernateSessionFactory.currentSession();
session.beginTransaction();
session.save(people);
session.getTransaction().commit();
}
}
People.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 - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.gpdi.orm.object.People" table="People">
<id name="id" column="peopleId" unsaved-value="0" >
<generator class="increment">
</generator>
</id>
<property name="name" column="name"></property>
<set name="addresses" cascade="save-update">
<key column="peopleId" not-null="true" />
<one-to-many class="com.gpdi.orm.object.Address"/>
</set>
</class>
</hibernate-mapping>
People.java
package com.gpdi.orm.object;
import java.util.HashSet;
import java.util.Set;
public class People implements java.io.Serializable
{
private long id;
private String name;
private Set addresses = new HashSet();
public Set getAddresses() {
return addresses;
}
public void setAddresses(Set addresses) {
this.addresses = addresses;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Address.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 - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.gpdi.orm.object.Address" table="Address">
<id name="id" column="addressId" unsaved-value="0">
<generator class="increment">
</generator>
</id>
<many-to-one name="people" column="peopleId" insert="false" update="false"></many-to-one>
<property name="addressName" column="addressName"></property>
<property name="codeNumber" column="codeNumber"></property>
</class>
</hibernate-mapping>
Address.java
package com.gpdi.orm.object;
public class Address implements java.io.Serializable
{
private long id;
private People people;
private String addressName;
private String codeNumber;
public String getAddressName() {
return addressName;
}
public void setAddressName(String addressName) {
this.addressName = addressName;
}
public String getCodeNumber() {
return codeNumber;
}
public void setCodeNumber(String codeNumber) {
this.codeNumber = codeNumber;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public People getPeople() {
return people;
}
public void setPeople(People people) {
this.people = people;
}
}
下载例子:整理后
一对多的完整例子
下载例子:整理前