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.  [ 6 posts ] 
Author Message
 Post subject: Where are these values coming from??!
PostPosted: Wed Nov 29, 2006 10:41 am 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
Hi

I've got a class A which contains a collection of objects B. I add a load of B objects to the collection, then save A.

Looking at the debug, it seems to be doing an INSERT for each instance of B, which is great.

But then, it goes and does an UPDATE for each one too. And the values it uses are wrong.

Could anyone tell me a) why the updates are being done in the first place and b) where it's getting the values for the column CHECKLIST_ITEM_ID. These are supposed to be foreign keys to another table. In the INSERT, the correct values are being used, but on the update, a FK Violation takes place, and I get a stacktrace.

The incorrect values that it uses are incremented by 1 each time, so I'm guessing it's the position of the record in the list. But that's just a guess.

The B records are getting stored in the DB, so I'd be happy if I could simply disable the UPDATES.

Here's my mapping:


Code:
    <class name="A" table="CHECKLIST">
      ...
      <list name="checklistItemRecords" table="CHECKLIST_ITEM_RECORD" lazy="false" fetch="select" outer-join="false" cascade="all">
         <key column="CHECKLIST_ID" />         
         <list-index column="[b][color=red]CHECKLIST_ITEM_ID[/color][/b]"/>
         <one-to-many class="com.lsb.uk.mqs.value.ChecklistItemRecordValue"/>
      </list>
      
    </class>       
   
    <class name="B" table="CHECKLIST_ITEM_RECORD">
      <composite-id unsaved-value="any">
           <key-property name="checklistItemId" column="CHECKLIST_ITEM_ID"/>
           <key-property name="checklistId" column="CHECKLIST_ID"/>   
      </composite-id>            
      <property name="noteId" column="NOTE_ID"/>
      <property name="checked" column="CHECKED"/>
    </class>


Here's the (correct) INSERT that's being executed

DEBUG hibernate.SQL - insert into CHECKLIST_ITEM_RECORD (NOTE_ID, CHECKED, CHECKLIST_ITEM_ID, CHECKLIST_ID) values (?, ?, ?, ?)
DEBUG type.IntegerType - binding '0' to parameter: 1
DEBUG type.BooleanType - binding 'false' to parameter: 2
DEBUG type.IntegerType - binding '139' to parameter: 3
DEBUG type.IntegerType - binding '37' to parameter: 4

Here's the (incorrect) UPDATE

DEBUG hibernate.SQL - update CHECKLIST_ITEM_RECORD set CHECKLIST_ID=?, CHECKLIST_ITEM_ID=? where CHECKLIST_ITEM_ID=? and CHECKLIST_ID=?
DEBUG type.IntegerType - binding '37' to parameter: 1
DEBUG type.IntegerType - binding '0' to parameter: 2
DEBUG type.IntegerType - binding '120' to parameter: 3
DEBUG type.IntegerType - binding '37' to parameter: 4

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 11:01 am 
Beginner
Beginner

Joined: Tue Nov 21, 2006 5:18 am
Posts: 31
Location: Bangalore, India
Hi,
You have mapped same table(CHECKLIST_ITEM_RECORD) two times.

what is the use of this,

Instead of doing these, you can use set in class A and do one-to-many mapping.

Otherwise, could you explain, what you want exaclty, and post your java source, which you use for insert

_________________
persist_coder
--credit please if it helps you


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 11:20 am 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
Hi

Thanks for replying. I'm new to Hibernate, so if you see any obvious looking mistakes in my mappings (like mapping a table twice) please tell me, because the chances are I will not be aware of them (so thanks for that pointer).

All I want is a LIST of ChecklistItemRecordValue objects hanging off my parent ChecklistValue object. It should look like this:

[A]>---[B]----[C]

Where A is ChecklistValue, B is ChecklistItemRecordValue. The value of CHECKLIST_ITEM_ID is a foreign key to another table (C). B also contains other fields (such as 'checked')

This is the code I'm using to save the object

Code:
   public ChecklistValue saveChecklist(ChecklistValue checklist) {
      final String mn = "saveChecklist";
      log.debug(mn);
      updateLastModifiedOn(checklist);
      if(checklist.getChecklistId() < 1) {
         Integer id = (Integer)getHibernateTemplate().save(checklist);
         checklist.setChecklistId(id.intValue());
      }
      else {
         getHibernateTemplate().update(checklist);
      }
      return findChecklistByPrimaryKey(checklist.getChecklistId());
      
   }


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 11:48 am 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
NB the saveChecklist method is calling the line

Code:
getHibernateTemplate().update(checklist);


because I previously saved the top level object

I also changed the line below so the portion in red is not there any more (I think this fixes the 'table mapped twice' problem), but I still see the same behaviour.

<list name="checklistItemRecords" table="CHECKLIST_ITEM_RECORD" lazy="false" fetch="select" outer-join="false" cascade="all">

-Richard


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 12:10 pm 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
I'm sorry, it's been a long day.

The relationship should be:

[A]---<[B]----[C]

not:

[A]>---[B]----[C]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 5:03 am 
Beginner
Beginner

Joined: Tue Nov 21, 2006 5:18 am
Posts: 31
Location: Bangalore, India
Hi,

You can do like these,

<class name="A" table="CHECKLIST">
...
<set name="checklistItemRecords" lazy="false" cascade="all">
<key column="CHECKLIST_ID" />
<key column="CHECKLIST_ITEM_ID"/>
<one-to-many class="com.lsb.uk.mqs.value.ChecklistItemRecordValue"/>
</set>

</class>

<class name="ChecklistItemRecordValue" table="CHECKLIST_ITEM_RECORD">
<composite-id unsaved-value="any">
<key-property name="checklistItemId" column="CHECKLIST_ITEM_ID"/>
<key-property name="checklistId" column="CHECKLIST_ID"/>
</composite-id>
<property name="noteId" column="NOTE_ID"/>
<property name="checked" column="CHECKED"/>
</class>

For this insert will happen for two times, one is for A class and ChecklistItemRecordValue class.

During insertion and updation be sure, in class A's checklistItemRecords are not null

_________________
persist_coder
--credit please if it helps you


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.