I have an object mapped to the database which has a semi-complex relational property on it. This property works fine in unit testing. However, when I attempt to do an HQL query that uses this property in a read-only fashion, Hibernate attempts to update the data in the database!
Any ideas how this could be happening, and how I would prevent it? Is the relationship mis-mapped?
The mapping:
Code:
<class name="Product" table="products">
...
<set name="childPrices" table="package_prices" access="field">
<key column="package_id" not-null="true" />
<composite-element class="PackagePrice">
<many-to-one name="product" column="product_id"
access="field" not-null="true" class="Product" />
<nested-composite-element name="price"
class="Price" access="field">
<property name="amount" access="field"
not-null="true" />
<many-to-one name="currency" column="currency_id"
access="field" not-null="true" class="com.risi.pricing.Currency" />
</nested-composite-element>
</composite-element>
</set>
...
</class>
This query is not a problem:
Code:
select distinct p from Product p
where p.deleted=0
This query is:
Code:
select distinct p from Product p
where p.deleted=0
and p.childPrices.size > 0
And causes this error:
Code:
com.mycompany.DaoException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
...
Caused by: java.sql.BatchUpdateException: null, message from server: "Cannot add or update a child row: a foreign key constraint fails"
The failure is because Hibernate is attempting to insert nulls into non-null fields. But the point is, it shouldn't be attempting to insert/update anything in the first place - this is a select query.