此範例為雙向一對多關聯1.建立表格:
drop table if exists t_address02;drop table if exists t_user02;drop table if exists t_group02;create table t_group02(id int(3),name varchar(30),primary key(id));create table t_user02(id int(11) auto_increment,name varchar(50) default '',age int(4),group_id int(3),primary key(id),foreign key(group_id) references t_group02(id));create table t_address02(id numeric(6),address varchar(200) default '',zipcode varchar(10) default '',tel varchar(20) default '',type varchar(20) default '',user_id int(11),idx int(11),primary key(id),foreign key(user_id) references t_user02(id));2.建立TUser02.hbm.xml、TGroup02.hbm.xml、TAddress02.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"><!-- Generated 2009/8/9 ?W?? 09:27:03 by Hibernate Tools 3.2.4.GA --><hibernate-mapping><class name="org.redsaga.relation.TUser02" table="t_user02" catalog="manning"><id name="id" type="java.lang.Integer"><column name="id" /><generator class="identity" /></id><many-to-one name="TGroup02" class="org.redsaga.relation.TGroup02" outer-join="auto" update="true" insert="true" cascade="none"><column name="group_id" /></many-to-one><property name="name" type="string"><column name="name" length="50" /></property><property name="age" type="java.lang.Integer"><column name="age" /></property><set name="TAddress02s" inverse="true" lazy="true" table="t_address02" cascade="all" order-by="zipcode asc" fetch="select"><key><column name="user_id" /></key><one-to-many class="org.redsaga.relation.TAddress02" /></set></class></hibernate-mapping>==========================================<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2009/8/9 ?W?? 09:27:03 by Hibernate Tools 3.2.4.GA --><hibernate-mapping><class name="org.redsaga.relation.TGroup02" table="t_group02" catalog="manning"><id name="id" type="java.lang.Integer"><column name="id" /><generator class="assigned" /></id><property name="name" type="string"><column name="name" length="30" /></property><set name="TUser02s" inverse="true" lazy="true" table="t_user02" fetch="select"><key><column name="group_id" /></key><one-to-many class="org.redsaga.relation.TUser02" /></set></class></hibernate-mapping>==========================================<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2009/8/9 ?W?? 09:27:03 by Hibernate Tools 3.2.4.GA --><hibernate-mapping><class name="org.redsaga.relation.TAddress02" table="t_address02" catalog="manning"><id name="id" type="int"><column name="id" precision="6" scale="0" /><generator class="assigned" /></id><many-to-one name="TUser02" class="org.redsaga.relation.TUser02" cascade="none" outer-join="auto" not-null="true" update="true" insert="true"><column name="user_id" /></many-to-one><property name="address" type="string"><column name="address" length="200" /></property><property name="zipcode" type="string"><column name="zipcode" length="10" /></property><property name="tel" type="string"><column name="tel" length="20" /></property><property name="type" type="string"><column name="type" length="20" /></property><property name="idx" type="java.lang.Integer"><column name="idx" /></property></class></hibernate-mapping>==========================================3.建立TUser02.java、TGroup02.java、TAddress02.java:package org.redsaga.relation;// Generated 2009/8/9 上午 09:27:02 by Hibernate Tools 3.2.4.GAimport java.util.HashSet;import java.util.Set;/*** TUser02 generated by hbm2java*/public class TUser02 implements java.io.Serializable {private Integer id;private TGroup02 TGroup02;private String name;private Integer age;private Set TAddress02s = new HashSet(0);public TUser02() {}public TUser02(TGroup02 TGroup02, String name, Integer age, Set TAddress02s) {this.TGroup02 = TGroup02;this.name = name;this.age = age;this.TAddress02s = TAddress02s;}public Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}public TGroup02 getTGroup02() {return this.TGroup02;}public void setTGroup02(TGroup02 TGroup02) {this.TGroup02 = TGroup02;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public Integer getAge() {return this.age;}public void setAge(Integer age) {this.age = age;}public Set getTAddress02s() {return this.TAddress02s;}public void setTAddress02s(Set TAddress02s) {this.TAddress02s = TAddress02s;}}========================================package org.redsaga.relation;// Generated 2009/8/9 上午 09:27:02 by Hibernate Tools 3.2.4.GAimport java.util.HashSet;import java.util.Set;/*** TGroup02 generated by hbm2java*/public class TGroup02 implements java.io.Serializable {private Integer id;private String name;private Set TUser02s = new HashSet(0);public TGroup02() {}public TGroup02(String name, Set TUser02s) {this.name = name;this.TUser02s = TUser02s;}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 Set getTUser02s() {return this.TUser02s;}public void setTUser02s(Set TUser02s) {this.TUser02s = TUser02s;}}=========================================package org.redsaga.relation;// Generated 2009/8/9 上午 09:27:02 by Hibernate Tools 3.2.4.GA/*** TAddress02 generated by hbm2java*/public class TAddress02 implements java.io.Serializable {private int id;private TUser02 TUser02;private String address;private String zipcode;private String tel;private String type;private Integer idx;public TAddress02() {}public TAddress02(int id) {this.id = id;}public TAddress02(int id, TUser02 TUser02, String address, String zipcode,String tel, String type, Integer idx) {this.id = id;this.TUser02 = TUser02;this.address = address;this.zipcode = zipcode;this.tel = tel;this.type = type;this.idx = idx;}public int getId() {return this.id;}public void setId(int id) {this.id = id;}public TUser02 getTUser02() {return this.TUser02;}public void setTUser02(TUser02 TUser02) {this.TUser02 = TUser02;}public String getAddress() {return this.address;}public void setAddress(String address) {this.address = address;}public String getZipcode() {return this.zipcode;}public void setZipcode(String zipcode) {this.zipcode = zipcode;}public String getTel() {return this.tel;}public void setTel(String tel) {this.tel = tel;}public String getType() {return this.type;}public void setType(String type) {this.type = type;}public Integer getIdx() {return this.idx;}public void setIdx(Integer idx) {this.idx = idx;}}========================================4.建立JUnit測試Method(testOneToMoreBidirect):/*** testOneToMoreBidirect 雙向一對多關聯* @throws Exception*/public void testOneToMoreBidirect() throws Exception {Transaction tran = null;TUser02 user = null;TAddress02 address = null;TGroup02 group = null;Set addrs = new HashSet(0);List list = null;Iterator it = null;Iterator it1 = null;Integer InId = new Integer(0);String hql = null;String groupName = null;boolean groupFlag = false;Object[] results = null;try{hql = "select max(id)+1 as id from TUser02";it = session.createQuery(hql).list().iterator();if(it.hasNext()){InId = (Integer)it.next();if(InId==null){InId = new Integer(0);}}address = new TAddress02();group = new TGroup02();user = new TUser02(group,"陳明田",35,addrs);user.setId(InId);groupName = "SalesGroup";hql = "select id from TGroup02 where name=?";Query query = session.createQuery(hql);query.setString(0, groupName);it = query.list().iterator();if(it.hasNext()){InId = (Integer)it.next();if(InId == null){InId = new Integer(0);}else{groupFlag = true;}}group.setId(InId);group.setName(groupName);hql = "select max(id)+1 as id from TAddress02";it = session.createQuery(hql).list().iterator();if(it.hasNext()){InId = (Integer)it.next();if(InId==null){InId = new Integer(0);}}address.setTel("36326");address.setAddress("台灣");address.setZipcode("300");address.setId(InId);addrs.add(address);//互設關聯address.setTUser02(user);user.setTGroup02(group);tran = session.beginTransaction();//若GroupName不存在,則Insert一筆Groupif(groupFlag==false){session.save(group);}session.save(user);session.flush();tran.commit();list = session.createQuery(" from TUser02").list();it = list.iterator();while(it.hasNext()){user = (TUser02)it.next();System.out.println("User Name:"+user.getName());System.out.println("User Name:"+user.getTGroup02().getName());it1 = user.getTAddress02s().iterator();while(it1.hasNext()){address = (TAddress02)it1.next();System.out.println("User Address:"+address.getAddress());System.out.println("User Tel:"+address.getTel());System.out.println("User ZipCode:"+address.getZipcode());}}}catch(HibernateException e){e.printStackTrace();}}=====================================5.測試結果:5.2.Console結果:Hibernate: select max(tuser02x0_.id)+1 as col_0_0_ from manning.t_user02 tuser02x0_Hibernate: select tgroup02x0_.id as col_0_0_ from manning.t_group02 tgroup02x0_ where tgroup02x0_.name='SalesGroup'Hibernate: select max(taddress02x0_.id)+1 as col_0_0_ from manning.t_address02 taddress02x0_Hibernate: insert into manning.t_group02 (name, id) values (?, ?)Hibernate: insert into manning.t_user02 (group_id, name, age) values (?, ?, ?)Hibernate: select taddress02x_.id, taddress02x_.user_id as user2_13_, taddress02x_.address as address13_, taddress02x_.zipcode as zipcode13_, taddress02x_.tel as tel13_, taddress02x_.type as type13_, taddress02x_.idx as idx13_ from manning.t_address02 taddress02x_ where taddress02x_.id=?Hibernate: insert into manning.t_address02 (user_id, address, zipcode, tel, type, idx, id) values (?, ?, ?, ?, ?, ?, ?)Hibernate: select tuser02x0_.id as id11_, tuser02x0_.group_id as group2_11_, tuser02x0_.name as name11_, tuser02x0_.age as age11_ from manning.t_user02 tuser02x0_User Name:陳明田User Name:SalesGroupUser Address:台灣User Tel:36326User ZipCode:300
2009年8月9日 星期日
Hibernate實作資料關聯(二)─one-to-many
訂閱:
文章 (Atom)