ok - I figure I'm doing something wrong but here goes:
With the following code, I expect that 1 Sale object and 2 Item
objects will be saved to the database. Also, the 2 Items will
each have their parent order (field orderID) set to the Sale object.
This doesn't seem to work. If I load the first product from
the database and change anything on it, then a query is issued
during the flush process to set the orderID=null on the first
Item object.
The queries issued to the database are:
1. insert for sale
3. insert into item for item1
4. update to product for p1
5. update item set orderID=null where orderID=? and lineID=? [for item1]
6. inset into item for item2
I'm not sure why the 5th statement is being generated. Any thoughts?
I tried to summarize what I was seeing in the log, but I can always post
more from the actually log if necessary....
When I remove the statement p1.setInvUnitPrice(Money.dollars(15.25)), the
first item is updated correctly so that the orderID field points to the
Sale object.
Figures I find this on a Friday night.....
Thanks in advance,
Chris....
Code:
public static void main(String[] args) throws Exception {
//context contains the Session object which
//the DAO object retrieves in order to perform the lookup
//query
Context context = new Context();
Sale sale = new Sale();
context.getSession().save(sale);
Item item1 = new Item();
ProductDAO pd = new ProductDAO();
Product p1 = pd.loadBySKU(context, "widg1");
item1.setProduct(p1);
sale.addItem(item1);
p1.setInvUnitPrice(Money.dollars(15.25));
Item item2 = new Item();
Product p2 = pd.loadBySKU(context, "widg2");
item2.setProduct(p2);
sale.addItem(item2);
context.getSession().flush();
context.getSession().connection().commit();
}
Code:
<class name="com.itsolut.entity.order.IOrder"
table="order" discriminator-value="X" proxy="com.itsolut.entity.order.IOrder" >
<id name="orderID" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<discriminator column="type" type="string" length="2" />
<set name="items" lazy="true" cascade="save-update">
<key column="orderID"/>
<one-to-many class="com.itsolut.entity.order.Item"/>
</set>
<subclass name="com.itsolut.entity.order.Sale" discriminator-value="S"
proxy="com.itsolut.entity.order.Sale"/>
</class>
<class name="com.itsolut.entity.order.Item" table="order_item"
proxy="com.itsolut.entity.order.Item">
<id name="lineID" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<many-to-one name="product" class="com.itsolut.entity.product.Product" column="productID"/>
<property name="inventoryUnitPrice" type="com.itsolut.core.hibernate.type.MoneyType">
<column name="invPrice"/>
<column name="invCurrency"/>
</property>
<many-to-one name="order" column="orderID"
class="com.itsolut.entity.order.IOrder"/>
</class>
<class name="com.itsolut.entity.product.Product" table="product"
proxy="com.itsolut.entity.product.Product" discriminator-value="X">
<id name="prodID" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<discriminator column="type" type="char" length="1"/>
<subclass name="com.itsolut.entity.product.Inventorable" discriminator-value="I"
proxy="com.itsolut.entity.product.Inventorable">
<property name="invUnitPrice" type="com.itsolut.core.hibernate.type.MoneyType">
<column name="invMoney"/>
<column name="invCurrency"/>
</property>
</subclass>
</class>