接前文在Hibernate中配置一对多连接表
其中表roomusers更改如下:
映射文件配置如下:
Room.hbm.xml
Userinfo.hbm.xml
测试代码如下:
其中表roomusers更改如下:
create table ROOMUSERS(
USERID NUMBER(4),
ROOMID NUMBER(4) not null
);
USERID NUMBER(4),
ROOMID NUMBER(4) not null
);
映射文件配置如下:
Room.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 package="com.cosmow.hibernatedemo2.entity">
<class name="Room" table="ROOM" schema="STORE">
<id name="id" type="java.lang.Long">
<column name="ID" precision="4" scale="0" />
<!-- Oracle数据库增长方式序列 -->
<generator class="sequence">
<param name="sequence">ROOM_SEQ</param>
</generator>
</id>
<!-- 映射roomnumber属性 -->
<property name="roomnumber" type="java.lang.String">
<column name="ROOMNUMBER" length="11" not-null="true" />
</property>
<!-- 映射name属性 -->
<property name="name" type="java.lang.String">
<column name="NAME" length="32" not-null="true" />
</property>
<!-- 通过连接表的多端,因此需要table属性为roomusers -->
<set name="users" cascade="all" table="roomusers">
<!-- 该key的字段为连接表中的字段,作为外键 -->
<key column="roomid" />
<!-- 配置多对多 -->
<many-to-many class="Userinfo" column="userid" />
</set>
</class>
</hibernate-mapping>
<!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 package="com.cosmow.hibernatedemo2.entity">
<class name="Room" table="ROOM" schema="STORE">
<id name="id" type="java.lang.Long">
<column name="ID" precision="4" scale="0" />
<!-- Oracle数据库增长方式序列 -->
<generator class="sequence">
<param name="sequence">ROOM_SEQ</param>
</generator>
</id>
<!-- 映射roomnumber属性 -->
<property name="roomnumber" type="java.lang.String">
<column name="ROOMNUMBER" length="11" not-null="true" />
</property>
<!-- 映射name属性 -->
<property name="name" type="java.lang.String">
<column name="NAME" length="32" not-null="true" />
</property>
<!-- 通过连接表的多端,因此需要table属性为roomusers -->
<set name="users" cascade="all" table="roomusers">
<!-- 该key的字段为连接表中的字段,作为外键 -->
<key column="roomid" />
<!-- 配置多对多 -->
<many-to-many class="Userinfo" column="userid" />
</set>
</class>
</hibernate-mapping>
Userinfo.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 package="com.cosmow.hibernatedemo2.entity">
<class name="Userinfo" table="USERINFO" schema="STORE">
<id name="id" type="java.lang.Long">
<column name="ID" precision="4" scale="0" />
<!-- Oracle数据库增长方式序列 -->
<generator class="sequence">
<param name="sequence">USERINFO_SEQ</param>
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="32" not-null="true" />
</property>
<property name="sex" type="java.lang.String">
<column name="SEX" length="2" />
</property>
<set name="rooms" cascade="all" table="roomusers">
<key column="userid" />
<many-to-many class="Room" column="roomid" />
</set>
</class>
</hibernate-mapping>
<!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 package="com.cosmow.hibernatedemo2.entity">
<class name="Userinfo" table="USERINFO" schema="STORE">
<id name="id" type="java.lang.Long">
<column name="ID" precision="4" scale="0" />
<!-- Oracle数据库增长方式序列 -->
<generator class="sequence">
<param name="sequence">USERINFO_SEQ</param>
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="32" not-null="true" />
</property>
<property name="sex" type="java.lang.String">
<column name="SEX" length="2" />
</property>
<set name="rooms" cascade="all" table="roomusers">
<key column="userid" />
<many-to-many class="Room" column="roomid" />
</set>
</class>
</hibernate-mapping>
测试代码如下:
/**
* Demo.java
*
* Copyright 2008. All Rights Reserved.
*/
package com.cosmow.hibernatedemo2.demo;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.cosmow.HibernateSessionFactory;
import com.cosmow.hibernatedemo2.entity.Room;
import com.cosmow.hibernatedemo2.entity.Userinfo;
/**
* TODO
*
* Revision History
*
* 2008-7-11,Cosmo,created it
*/
public class Demo {
public void test() {
//第一个room实体
Room room = new Room();
room.setName("cosmoHome");
room.setRoomnumber("001");
//第二个room实体
Room room2 = new Room();
room2.setName("Home2");
room2.setRoomnumber("002");
//第一个user实体
Userinfo user = new Userinfo();
user.setName("cosmo");
user.setSex("M");
//第二个user实体
Userinfo user2 = new Userinfo();
user2.setName("winnie");
user2.setSex("F");
//创建第二个user实体与第二个room实体的关系,通过连接表维护关系
Set<Room> roomSet = new HashSet<Room>();
roomSet.add(room2);
user2.setRooms(roomSet);
//创建第一个room实体与第一、第二个user实体的关系,通过连接表维护关系
//添加user实体到集合userSet
Set<Userinfo> userSet = new HashSet<Userinfo>();
userSet.add(user);
userSet.add(user2);
room.setUsers(userSet);
// 启动Session
Session session = HibernateSessionFactory.getSession();
// 启动事务
Transaction tx = session.beginTransaction();
// 持久化room实体
//此时将更新表room和userinfo以及维护关系的roomusers表
session.save(room);
tx.commit();
HibernateSessionFactory.closeSession();
// 启动Session
session = HibernateSessionFactory.getSession();
// 启动事务
tx = session.beginTransaction();
//获取Room实体以成为持久化状态,此时将获取room表的所有字段信息
Room roomF1 = (Room)session.get(Room.class, room.getId());
//通过外连接roomusers获取userinfo表对应字段信息
Iterator<Userinfo> iF1 = roomF1.getUsers().iterator();
while(iF1.hasNext()) {
Userinfo userF = iF1.next();
System.out.println(userF.getId());
System.out.println(userF.getName());
System.out.println(userF.getSex());
}
Room roomF2 = (Room)session.get(Room.class, room2.getId());
Iterator<Userinfo> iF2 = roomF2.getUsers().iterator();
while(iF1.hasNext()) {
Userinfo userF = iF2.next();
System.out.println(userF.getId());
System.out.println(userF.getName());
System.out.println(userF.getSex());
}
tx.commit();
HibernateSessionFactory.closeSession();
}
public static void main(String[] args) {
Demo demo = new Demo();
demo.test();
}
}
* Demo.java
*
* Copyright 2008. All Rights Reserved.
*/
package com.cosmow.hibernatedemo2.demo;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.cosmow.HibernateSessionFactory;
import com.cosmow.hibernatedemo2.entity.Room;
import com.cosmow.hibernatedemo2.entity.Userinfo;
/**
* TODO
*
* Revision History
*
* 2008-7-11,Cosmo,created it
*/
public class Demo {
public void test() {
//第一个room实体
Room room = new Room();
room.setName("cosmoHome");
room.setRoomnumber("001");
//第二个room实体
Room room2 = new Room();
room2.setName("Home2");
room2.setRoomnumber("002");
//第一个user实体
Userinfo user = new Userinfo();
user.setName("cosmo");
user.setSex("M");
//第二个user实体
Userinfo user2 = new Userinfo();
user2.setName("winnie");
user2.setSex("F");
//创建第二个user实体与第二个room实体的关系,通过连接表维护关系
Set<Room> roomSet = new HashSet<Room>();
roomSet.add(room2);
user2.setRooms(roomSet);
//创建第一个room实体与第一、第二个user实体的关系,通过连接表维护关系
//添加user实体到集合userSet
Set<Userinfo> userSet = new HashSet<Userinfo>();
userSet.add(user);
userSet.add(user2);
room.setUsers(userSet);
// 启动Session
Session session = HibernateSessionFactory.getSession();
// 启动事务
Transaction tx = session.beginTransaction();
// 持久化room实体
//此时将更新表room和userinfo以及维护关系的roomusers表
session.save(room);
tx.commit();
HibernateSessionFactory.closeSession();
// 启动Session
session = HibernateSessionFactory.getSession();
// 启动事务
tx = session.beginTransaction();
//获取Room实体以成为持久化状态,此时将获取room表的所有字段信息
Room roomF1 = (Room)session.get(Room.class, room.getId());
//通过外连接roomusers获取userinfo表对应字段信息
Iterator<Userinfo> iF1 = roomF1.getUsers().iterator();
while(iF1.hasNext()) {
Userinfo userF = iF1.next();
System.out.println(userF.getId());
System.out.println(userF.getName());
System.out.println(userF.getSex());
}
Room roomF2 = (Room)session.get(Room.class, room2.getId());
Iterator<Userinfo> iF2 = roomF2.getUsers().iterator();
while(iF1.hasNext()) {
Userinfo userF = iF2.next();
System.out.println(userF.getId());
System.out.println(userF.getName());
System.out.println(userF.getSex());
}
tx.commit();
HibernateSessionFactory.closeSession();
}
public static void main(String[] args) {
Demo demo = new Demo();
demo.test();
}
}