初学Hibernate很多人搞不清inverse的意义在这里做一个详细的解释
首先对inverse做一个语言描述
inverse="true"即表示放弃对关系的维护权.在inverse所在的hibernate配置元素中,所有的关系改变将不被反映到数据库中.
例如,如下两个配置
1 Item类映射文件
<?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 2010-4-13 15:36:15 by Hibernate Tools 3.3.0.GA --> <hibernate-mapping> <class name="com.hib.entity.Item" table="item" catalog="hibernate"> <id name="itemId" type="java.lang.Integer"> <column name="ITEM_ID" /> <generator class="identity" /> </id> <property name="sellerId" type="int"> <column name="SELLER_ID" not-null="true" /> </property> <property name="name" type="string"> <column name="NAME" not-null="true" /> </property> <set name="bids" table="bid" inverse="true" lazy="false" fetch="select"> <key> <column name="ITEM_ID" not-null="true" /> </key> <one-to-many class="com.hib.entity.Bid" /> </set> </class> </hibernate-mapping>
2 Bid类映射文件
<?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 2010-4-13 15:36:15 by Hibernate Tools 3.3.0.GA --> <hibernate-mapping> <class name="com.hib.entity.Bid" table="bid" catalog="hibernate"> <id name="bidId" type="java.lang.Integer"> <column name="BID_ID" /> <generator class="identity" /> </id> <many-to-one name="item" class="com.hib.entity.Item" fetch="select"> <column name="ITEM_ID" not-null="true" /> </many-to-one> <property name="bidderId" type="java.lang.Integer"> <column name="BIDDER_ID" /> </property> <property name="amount" type="java.lang.Integer"> <column name="AMOUNT" /> </property> </class> </hibernate-mapping>
Item类映射文件中set元素的inverse="true"表示 Item 类的 set 集合放弃对关联关系的维护权.也就是说,对于一个Item类的实例.改变set中的内容,在Item 的 update 和 insert 操作中将不被反映到数据库.这句话是什么意思.请看如下代码.
已知条件:数据库中.ITEM表和BID表数据如下
ITEM 表
--------------------------------------
ITEM_ID | NAME
---------------------------------------
1 | item1
--------------------------------------
2 | item2
---------------------------------------
BID表
----------------------------------------
BID_ID | ITEM_ID | AMOUNT
------------------------------------------
1 | 1 | 3000
--------------------------------------------
2 | 1 | 3000
-------------------------------------------
执行如下java代码
Item item=ItemDAO.getById(2); Bid bid=BidDAO.getById(2); item.getBids().add(bid); ItemDAO.update(item);
执行完后,数据库内容如下
ITEM 表
--------------------------------------
ITEM_ID | NAME
---------------------------------------
1 | item1
--------------------------------------
2 | item2
---------------------------------------
BID表
----------------------------------------
BID_ID | ITEM_ID | AMOUNT
------------------------------------------
1 | 1 | 3000
--------------------------------------------
2 | 1 | 3000
-------------------------------------------
可以看到,数据库没有任何改变.
查看
Item item=ItemDAO.getById(2); System.out.println(item.getBids().size());
size
Item item=ItemDAO.getById(2); System.out.println(item.getBids().size());
仍然为0.再执行如下代码
Item item=ItemDAO.getById(2); Bid bid=BidDAO.getById(2); bid.setItem(item) BidDAO.update(bid);
此时数据库为
ITEM 表
--------------------------------------
ITEM_ID | NAME
---------------------------------------
1 | item1
--------------------------------------
2 | item2
---------------------------------------
BID表
----------------------------------------
BID_ID | ITEM_ID | AMOUNT
------------------------------------------
1 | 1 | 3000
--------------------------------------------
2 | 2 | 3000
-------------------------------------------
再执行
Item item=ItemDAO.getById(2); System.out.println(item.getBids().size());
size为1,从上面可以看出.inverse="true"标志着Item类不维护关联关系(不反映到数据库).而有Bid类进行维护(反映到数据库)
如果需要让Item类来维护关联关系(Bid类必须放弃对关联关系的维护)修改配置文件如下
Item 类配置文件,修改inverse="false"
<?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 2010-4-13 15:36:15 by Hibernate Tools 3.3.0.GA --> <hibernate-mapping> <class name="com.hib.entity.Item" table="item" catalog="hibernate"> <id name="itemId" type="java.lang.Integer"> <column name="ITEM_ID" /> <generator class="identity" /> </id> <property name="sellerId" type="int"> <column name="SELLER_ID" not-null="true" /> </property> <property name="name" type="string"> <column name="NAME" not-null="true" /> </property> <set name="bids" table="bid" inverse="false" lazy="false" fetch="select"> <key> <column name="ITEM_ID" not-null="true" /> </key> <one-to-many class="com.hib.entity.Bid" /> </set> </class> </hibernate-mapping>
Bid 类映射文件,修改<many-to-one>元素,添加update="false",insert="false"属性,其意义为,在Bid类的update与insert操作中,忽略<many-to-one>所映射的item字段,不将其反映到数据库中
<?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 2010-4-13 15:36:15 by Hibernate Tools 3.3.0.GA --> <hibernate-mapping> <class name="com.hib.entity.Bid" table="bid" catalog="hibernate"> <id name="bidId" type="java.lang.Integer"> <column name="BID_ID" /> <generator class="identity" /> </id> <many-to-one name="item" class="com.hib.entity.Item" fetch="select" insert="false" update="false"> <column name="ITEM_ID" not-null="true" /> </many-to-one> <property name="bidderId" type="java.lang.Integer"> <column name="BIDDER_ID" /> </property> <property name="amount" type="java.lang.Integer"> <column name="AMOUNT" /> </property> </class> </hibernate-mapping>