-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: HQL select query somehow causes an update?
PostPosted: Tue Aug 16, 2005 2:57 pm 
Newbie

Joined: Mon Jun 13, 2005 6:31 pm
Posts: 9
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 3:12 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
I've noticed through debugging, that hibernate may not do a sql insert/update for a previous update in the same session, till it gets to the next query. In effect it doesn't run the updates untill it has to.

So, this could be from a previous update in the same session.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 11:08 am 
Newbie

Joined: Tue Oct 25, 2005 10:53 am
Posts: 1
I am having the same issue on a class mapped similarly. The class has a few sets of composite-elements, and when I do a vanilla query, it does updates, as well as deleting and reinserting all of the composite-elements.

This is on the first query that is being run within my process, so it can't be from a previous session.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 11:37 am 
Newbie

Joined: Mon Jun 13, 2005 6:31 pm
Posts: 9
I never understood the exact cause enough to articulate it clearly, but I believe that my mapping or table structure were somehow incorrect. After rethinking through my needs and the best way to meet them I ended up redesigning the table structure and domain model for this particular relationship. The mapping I created for that new structure worked without the problem.

The only advice I can give is to look closely at your table structures and your mapping. Good luck!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.