连接表
连接表就是保存多个关系在一张表中,而实体表中将不会出现与关系有关的任何信息。
1.表结构如下:
create table ROOM (
ID NUMBER(4) not null,
ROOMNUMBER VARCHAR2(11) not null,
NAME VARCHAR2(32) not null,
constraint ROOM_PK primary key (ID)
);
ID NUMBER(4) not null,
ROOMNUMBER VARCHAR2(11) not null,
NAME VARCHAR2(32) not null,
constraint ROOM_PK primary key (ID)
);
USERINFO(用户表)
create table USERINFO(
ID NUMBER(4) not null,
NAME VARCHAR2(32) not null,
SEX VARCHAR2(2),
constraint USERINFO_PK primary key (ID)
);
ID NUMBER(4) not null,
NAME VARCHAR2(32) not null,
SEX VARCHAR2(2),
constraint USERINFO_PK primary key (ID)
);
连接表
create table ROOMUSERS(
USERID NUMBER(4) not null,
ROOMID NUMBER(4) not null,
constraint ROOMUSERS_PK primary key (USERID)
);
USERID NUMBER(4) not null,
ROOMID NUMBER(4) not null,
constraint ROOMUSERS_PK primary key (USERID)
);
一对多连接表映射文件
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" />
<!-- 配置多对多 -->
<!-- 但是unique属性为true,表示userid字段为不可重复,保证一对多关系 -->
<many-to-many class="Userinfo" column="userid"
unique="true" />
</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" />
<!-- 配置多对多 -->
<!-- 但是unique属性为true,表示userid字段为不可重复,保证一对多关系 -->
<many-to-many class="Userinfo" column="userid"
unique="true" />
</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>
<!-- 使用join来配置多对一的连接,以table属性来表示连接表的多对一,连接表为roomusers -->
<!-- optional属性表示这是一个外连接,inverse属性可以出现在一端和多端,这里选择出现在多端,效果相同 -->
<join table="roomusers" optional="true" inverse="true">
<!-- 该key的字段为连接表中的字段,作为外键 -->
<key column="userid" />
<!-- 连接表中配置多对一,对应的字段为roomid -->
<many-to-one name="room" column="roomid" not-null="true" />
</join>
</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>
<!-- 使用join来配置多对一的连接,以table属性来表示连接表的多对一,连接表为roomusers -->
<!-- optional属性表示这是一个外连接,inverse属性可以出现在一端和多端,这里选择出现在多端,效果相同 -->
<join table="roomusers" optional="true" inverse="true">
<!-- 该key的字段为连接表中的字段,作为外键 -->
<key column="userid" />
<!-- 连接表中配置多对一,对应的字段为roomid -->
<many-to-one name="room" column="roomid" not-null="true" />
</join>
</class>
</hibernate-mapping>
使用连接表最大的不同如下:
一端:
1.在<set>元素中需要增加table属性,以表示这是一个连接表的关系。
2.将不使用连接表一对多的<one-to-many>,改为<many-to-many>,并增加unique属性
3.在<set>元素中的所有字段都将针对连接表(table属性所指定的表)
多端:
1.使用<join>元素来配置多端的连接。
2.在<join>元素中,所有字段都针对连接表(table属性所指定的表)
测试代码如下:
实体类Room
package com.cosmow.hibernatedemo2.entity;
import java.util.Set;
/**
* Room generated by MyEclipse Persistence Tools
*/
public class Room implements java.io.Serializable {
// Fields
private static final long serialVersionUID = 1L;
private Long id;
private String roomnumber;
private String name;
private Set<Userinfo> users;
// Constructors
/** default constructor */
public Room2() {
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoomnumber() {
return this.roomnumber;
}
public void setRoomnumber(String roomnumber) {
this.roomnumber = roomnumber;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set<Userinfo> getUsers() {
return users;
}
public void setUsers(Set<Userinfo> users) {
this.users = users;
}
}
import java.util.Set;
/**
* Room generated by MyEclipse Persistence Tools
*/
public class Room implements java.io.Serializable {
// Fields
private static final long serialVersionUID = 1L;
private Long id;
private String roomnumber;
private String name;
private Set<Userinfo> users;
// Constructors
/** default constructor */
public Room2() {
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoomnumber() {
return this.roomnumber;
}
public void setRoomnumber(String roomnumber) {
this.roomnumber = roomnumber;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set<Userinfo> getUsers() {
return users;
}
public void setUsers(Set<Userinfo> users) {
this.users = users;
}
}
实体类Userinfo
package com.cosmow.hibernatedemo2.entity;
/**
* Userinfo generated by MyEclipse Persistence Tools
*/
public class Userinfo implements java.io.Serializable {
// Fields
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String sex;
private Room2 room;
// Constructors
/** default constructor */
public Userinfo() {
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Room2 getRoom() {
return room;
}
public void setRoom(Room2 room) {
this.room = room;
}
}
/**
* Userinfo generated by MyEclipse Persistence Tools
*/
public class Userinfo implements java.io.Serializable {
// Fields
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String sex;
private Room2 room;
// Constructors
/** default constructor */
public Userinfo() {
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Room2 getRoom() {
return room;
}
public void setRoom(Room2 room) {
this.room = room;
}
}
test
public void test() {
Room room = new Room();
room.setName("cosmoHome");
room.setRoomnumber("001");
Userinfo user = new Userinfo();
user.setName("cosmo");
user.setSex("M");
user.setRoom(room);
Set<Userinfo> userSet = new HashSet<Userinfo>();
userSet.add(user);
room.setUsers(userSet);
// 启动Session
Session session = HibernateSessionFactory.getSession();
// 启动事务
Transaction tx = session.beginTransaction();
// 持久化room实体
session.save(room);
tx.commit();
HibernateSessionFactory.closeSession();
// 启动Session
session = HibernateSessionFactory.getSession();
// 启动事务
tx = session.beginTransaction();
Room room1 = (Room)session.get(Room.class, room.getId());
session.delete(room1);
tx.commit();
HibernateSessionFactory.closeSession();
}
Room room = new Room();
room.setName("cosmoHome");
room.setRoomnumber("001");
Userinfo user = new Userinfo();
user.setName("cosmo");
user.setSex("M");
user.setRoom(room);
Set<Userinfo> userSet = new HashSet<Userinfo>();
userSet.add(user);
room.setUsers(userSet);
// 启动Session
Session session = HibernateSessionFactory.getSession();
// 启动事务
Transaction tx = session.beginTransaction();
// 持久化room实体
session.save(room);
tx.commit();
HibernateSessionFactory.closeSession();
// 启动Session
session = HibernateSessionFactory.getSession();
// 启动事务
tx = session.beginTransaction();
Room room1 = (Room)session.get(Room.class, room.getId());
session.delete(room1);
tx.commit();
HibernateSessionFactory.closeSession();
}