Hi:
I store different products to one order table, I use <any>mapping.
ProductA and ProductB implements an interface 'Product' which define common setter/getter methods.
I also write test.ProductTypeMapper.java, (followed by the instruction in 122.html)
Code:
static public final Integer PRODUCT_A = new Integer(1);
static public final Integer PRODUCT_B = new Integer(2);
Add different Products to Order works fine, the problem is when I try to get orders from a ProductA object like this:
Code:
ProductA ap = (ProductA) sess.load(ProductA.class, new Long(1));
Set orders = ap.getOrders();
It also returns ProductB which productId=1! I do only need ProductA.
Hibernate output sql is:
Code:
select ... from Orders orders0_ where orders0_.product_id=?
Why Hibernate did NOT generate 'where orders0_.product_id=? and orders0_.product_type=PRODUCT_A'?
And how can I get all ProductA or ProductB orders from Order ?
(I mean: in SQL: select * from Order where product_type=PRODUCT_A)
Following is Order and other hbm:
Order.hbm.xml:
Code:
<class name="test.Order" table="Orders">
<id name="orderId" type="int" unsaved-value="null" >
<column name="order_id" sql-type="int" not-null="true"/>
<generator class="native"/>
</id>
<any name="product" id-type="int" meta-type="test.ProductTypeMapper">
<column name="product_type" />
<column name="product_id" />
</any>
<property name="client" column="client" type="string" />
<property name="orderTime" column="order_time" type="date" />
</class>
ProductA.hbm.xml
Code:
<class name="test.ProductA" table="product_A">
<id name="productId" type="int" unsaved-value="null" >
<column name="product_id" sql-type="int" not-null="true"/>
<generator class="native"/>
</id>
<property name="type" type="string" />
<set name="orders" lazy="true" inverse="true">
<key column="product_id" />
<one-to-many class="test.Order"/>
</set>
</class
ProductB.hbm.xml
Code:
<class name="test.ProductA" table="product_A">
<id name="productId" type="int" unsaved-value="null" >
<column name="product_id" sql-type="int" not-null="true"/>
<generator class="native"/>
</id>
<property name="type" type="string" />
<set name="orders" lazy="true" inverse="true">
<key column="product_id" />
<one-to-many class="test.Order"/>
</set>
</class
Thanks