Hibernate 各种映射用法
1. 值集合映射:这些集合包含的元素不是Domain Object,是一些值对象
1.1 Set映射:
<setname="Set属性名"table="表名">
<keycolumn="与主键关联字段名"/>
<elementtype="数据类型"column="字段名"/>
</set>
1.2List映射:
<listname="List属性名"table="表名">
<keycolumn="与主键关联字段名"/>
<list-indexcolumn="索引字段名"/>
<elementtype="数据类型"column="字段名"/>
</list>
1.3Map映射:
<mapname="Map属性名"table="表名">
<keycolumn="与主键关联字段名"/>
<map-keytype="数据类型"column="KEY字段名"/>
<elementtype="数据类型"column="VALUE字段名"/>
</map>1.4 Bag映射:
<bagname="bag属性名"table="表名">
<keycolumn="与主键关联字段名"/>
<elementtype="数据类型"column="字段名"/>
</bag>
1.5Idbag映射:
<idbag name="idbag 属性名" table="表名"><collection-id column="cid" type="java.lang.String">
<generator class="uuid.hex" />
</collection-id>
<key column="与主键关联字段名" />
<elementtype="数据类型"column="字段名"/>
</idbag>Bag和Set一样是无序集合,和Set不同的是,Bag允许元素重复,可以将List映射为Bag。
Bag与IdBag的区别:
public class User { private List<String> emails=new ArrayList<String>(); public void addEmail(String email) { emails.add(email); } public void removeEmail(String email) { emails.remove(email); } CREATE TABLE user ( id INT(11) NOT NULL auto_increment PRIMARY KEY ); CREATE TABLE emails( userId INT(11) NOT NULL, email VARCHAR(100) NOT NULL ); <bag name="emails" table="emails"> <key column="userId" /> <element column="email" type="java.lang.String" /> </bag> <idbag name="emails" table="emails"> <collection-id column="cid" type="java.lang.String"> <generator class="uuid.hex" /> </collection-id> <key column="email" /> <element column="email" type="java.lang.String" /> </idbag> User user1=new User(); user1.addEmail("[email protected]"); user1.addEmail("[email protected]"); user1.addEmail("[email protected]"); User user2=new User(); user2.addEmail("[email protected]"); //save user //user table id 1 2
如果采用bag,则emails表如下:
userid email
1email1
1email2
1email2
2 email3执行user1.remove(email2),根据List的remove方法,会删除第一个匹配的元素,但是Hibernate无法定位此元素,因而采取的方式为先全部清空,再重新插入数据库,效率低下,生成的sql意思如下:
delete from emails where userId=1;
insert into emails(userId,email) values(1,email1);
insert into emails(userId,email) values(1,email2);
insert into emails(userId,email) values(2,email3);
如果采用Idbag,则emails表如下:
cid userid email
cid11email1
cid21email2
cid31email2
cid4 2 email3执行user1.remove(email2),生成的sql意思如下:
delete from emails where cid=cid2;
此方式效率较高。
2. 实体关系映射
<many-to-one name="company" column="companyid" class="com.ijo.domain.user.Company">
<set name="subProjects" table="subproject_programmer" inverse="true">
<keycolumn="programmerid"/>
<many-to-manyclass="com.ijo.domain.project.SubProject"column="subprojectid"/>
</set><bagname="subscriptions"table="subscription">
<keycolumn="companyid"/>
<one-to-manyclass="com.ijo.domain.subscription.Subscription"/>
</bag>3. 继承关系映射
3.1 <subclass name="com.ijo.domain.common.category.AdjustCategory" discriminator -
value="AdjustCategory">
</subclass>
<subclass name="com.ijo.domain.common.category.TransportCategory" discriminator-value="TransportCategory"> </subclass>
3.2 <joined-subclass name="com.ijo.domain.user.CustomerUser" table="customeruser">
<keycolumn="userid"/>
</joined-subclass>