This is a possible bug I noticed. I am using the Hibernate CVS Head. Has anyone testing the latest, greatest and breakiest noticed this?
I have a PersistantSet which contains only a composite-element. However as soon as I call iterator() on the set I trigger some sort of update on the set.
AbstractPersistentCollection v1.15
PersistentSet v1.12
Mapping:
<set name="prices" table="product_price" sort="natural">
<key column="item_id"/>
<composite-element class="ProductPrice">
<property name="qty"/>
<property name="price"/>
</composite-element>
</set>
Table: (MySQL 4.1)
CREATE TABLE `product_price` (
`item_id` bigint(20) NOT NULL default '0',
`qty` int(11) NOT NULL default '0',
`price` float NOT NULL default '0',
UNIQUE KEY `item_id` (`item_id`,`qty`)
)
Java:
getPrices().iterator();
If the set is sorted (with sort="natural") It tries to do this SQL:
Hibernate: insert into product_price (item_id, qty, price) values (?, ?, ?)
Hibernate: insert into product_price (item_id, qty, price) values (?, ?, ?)
Hibernate: insert into product_price (item_id, qty, price) values (?, ?, ?)
Which produces a unique key error (the rows are already in the table and the set hasn't been modified by my code)
If the set isn't sorted (no sort attr) It does this SQL:
Hibernate: delete from product_price where item_id=? and qty=? and price=?
Hibernate: insert into product_price (item_id, qty, price) values (?, ?, ?)
Hibernate: delete from product_price where item_id=? and qty=? and price=?
Hibernate: insert into product_price (item_id, qty, price) values (?, ?, ?)
Hibernate: delete from product_price where item_id=? and qty=? and price=?
Hibernate: insert into product_price (item_id, qty, price) values (?, ?, ?)
Basically it deletes all the rows and reinserts them. This actually works, but seems superfluous.
If you don't use the iterator it doesn't do either. I presume it doesn't actually initialize the set contents either.
So am I the only one? Has anyone else seen this sort of behavior?
|