waysun一路阳光

不轻易服输,不轻言放弃.--心是梦的舞台,心有多大,舞台有多大。踏踏实实做事,认认真真做人。

  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理 ::
  167 随笔 :: 1 文章 :: 64 评论 :: 0 Trackbacks
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;
 }
}
下载例子:整理后
一对多的完整例子
下载例子:整理前

posted on 2009-02-22 19:36 weesun一米阳光 阅读(589) 评论(0)  编辑  收藏 所属分类: Hibernatecnweblog/nm1504

只有注册用户登录后才能发表评论。


网站导航: