Bag是集合,与Set不同的是,Bag允许重复的元素,
在Java的标准API中并没有提供Bag容器,Hibernate提供自己的Bag实现,允许您将List映射为Bag。
您可以如下定义User类别,其中的List成员将被用作Bag来使用,而不管物件在List容器中的顺序:
- User.java
package onlyfun.caterpillar;
import java.util.List;
public class User {
private Integer id;
private String name;
private List items;
// 必须要有一个预设的建构方法
// 以使得Hibernate可以使用Constructor.newInstance()建立物件
public User() {}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public void addItem(String item) {
items.add(item);
}
public void removeItem(String name) {
items.remove(name);
}
}
最简单的Bag映射是使用<bag>标签,在这之前,假设您如下建立表格:
CREATE TABLE user (
id INT(11) NOT NULL auto_increment PRIMARY KEY,
name VARCHAR(100) NOT NULL default ''
);
CREATE TABLE item (
id INT(11) NOT NULL,
name VARCHAR(100) NOT NULL
);
id INT(11) NOT NULL auto_increment PRIMARY KEY,
name VARCHAR(100) NOT NULL default ''
);
CREATE TABLE item (
id INT(11) NOT NULL,
name VARCHAR(100) NOT NULL
);
接着定义映射文件,如下所示:
- User.hbm.xml
java 代码
- <?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">
- <hibernate-mapping>
- <class name="onlyfun.caterpillar.User" table="user">
- <id name="id" column="id" type="java.lang.Integer">
- <generator class="native" />
- </id>
- <property name="name" column="name" type="java.lang.String" />
- <bag name="items" table="item">
- <key column="id" />
- <element column="name" type="java.lang.String" />
- </bag>
- </class>
- </hibernate-mapping>
假设您如下储存物件:
User user1 = new User();
user1.setItems(new ArrayList());
user1.setName("caterpillar");
user1.addItem("Java Gossip");
user1.addItem("Java Gossip");
user1.addItem("Caxxx A80");
User user2 = new User();
user2.setItems(new ArrayList());
user2.setName("momor");
user2.addItem("Snoppy world");
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(user1);
session.save(user2);
tx.commit();
session.close();
user1.setItems(new ArrayList());
user1.setName("caterpillar");
user1.addItem("Java Gossip");
user1.addItem("Java Gossip");
user1.addItem("Caxxx A80");
User user2 = new User();
user2.setItems(new ArrayList());
user2.setName("momor");
user2.addItem("Snoppy world");
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(user1);
session.save(user2);
tx.commit();
session.close();
则资料库中会有如下的资料:
mysql> select * from user; +----+-------------+ | id | name | +----+-------------+ | 1 | caterpillar | | 2 | momor | +----+-------------+ 2 rows in set (0.00 sec) mysql> select * from item; +----+--------------------+ | id | name | +----+-------------------+ | 1 | Java Gossip | | 1 | Java Gossip | | 1 | Caxxx A80 | | 2 | Snoppy world | +----+-------------------+ 4 rows in set (0.00 sec) |
您可以如下更新资料:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
User user = (User) session.load(User.class, new Integer(1));
user.removeItem("Java Gossip");
tx.commit();
session.close();
Transaction tx = session.beginTransaction();
User user = (User) session.load(User.class, new Integer(1));
user.removeItem("Java Gossip");
tx.commit();
session.close();
然而注意观察在更新资料时所使用的SQL:
Hibernate: delete from item where id=?
Hibernate: insert into item (id, name) values (?, ?)
Hibernate: insert into item (id, name) values (?, ?)
Hibernate: insert into item (id, name) values (?, ?)
Hibernate: insert into item (id, name) values (?, ?)
由于Bag的资料允许重复,当必须更新资料时,无法确定要更新的是哪一笔资料,因而采取的方式是删除集合物件对应的所有资料,然后重新将集合物件中的资料写入资料库,显然的这种作法相当的没有效率。
作为Bag的一种扩充,Hibernate提供idbag,藉由在定义Bag映射时加上"collection-id",让Hibernate可以直接确定所要更新的资料,提高资料库操作的效率,您可以先如下建立表格:
CREATE TABLE user (
id INT(11) NOT NULL auto_increment PRIMARY KEY,
name VARCHAR(100) NOT NULL default ''
);
CREATE TABLE item (
cid CHAR(32) NOT NULL,
id INT(11) NOT NULL,
name VARCHAR(100) NOT NULL
);
id INT(11) NOT NULL auto_increment PRIMARY KEY,
name VARCHAR(100) NOT NULL default ''
);
CREATE TABLE item (
cid CHAR(32) NOT NULL,
id INT(11) NOT NULL,
name VARCHAR(100) NOT NULL
);
其中item表格的cid就用于资料更新时定位之用,接着在映射文件中使用<idbag>标签加以定义:
- User.hbm.xml
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">
- <hibernate-mapping>
- <class name="onlyfun.caterpillar.User" table="user">
- <id name="id" column="id" type="java.lang.Integer">
- <generator class="native" />
- </id>
- <property name="name" column="name" type="java.lang.String" />
- <idbag name="items" table="item">
- <collection-id column="cid" type="java.lang.String">
- <generator class="uuid.hex" />
- </collection-id>
- <key column="id" />
- <element column="name" type="java.lang.String" />
- </idbag>
- </class>
- </hibernate-mapping>
使用上面用过的程式片段来储存物件的话,资料库中会有如下的资料:
mysql> select * from user; +----+-------------+ | id | name | +----+-------------+ | 1 | caterpillar | | 2 | momor | +----+-------------+ 2 rows in set (0.00 sec) mysql> select * from item; +------------------------------------------------+----+--------------------+ | cid | id | name | +------------------------------------------------+----+--------------------+ | 297eba61056726030105672605df0001 | 1 | Java Gossip | | 297eba61056726030105672605df0002 | 1 | Java Gossip | | 297eba61056726030105672605df0003 | 1 | Caxxx A80 | | 297eba61056726030105672605df0004 | 2 | Snoppy world | +------------------------------------------------+----+-------------------+ 4 rows in set (0.00 sec) |
如果使用上面提到过的程式片段来更新物件的话,则实际上Hibernate会使用以下的SQL来进行更新:
Hibernate: delete from item where cid=?
这一次并不是整个删除集合中的资料,而是直接藉由cid来确定所要更新的资料,比起只使用Bag,idbag的效率好了许多。