-->
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.  [ 9 posts ] 
Author Message
 Post subject: Unable to insert with one-to-many (w/composite id)
PostPosted: Fri Jul 25, 2008 6:47 pm 
Newbie

Joined: Fri Jul 25, 2008 6:35 pm
Posts: 6
I am unable to create any inserts with my children which have a composite id.

I am able to update existing children and can query -- I just cannot insert new ones.

It seems like a simple problem -- hopefully the solution is simple.

The Parent (ESM_DEVICE_INFO) has a one-to-many relationship with (ESM_DEVICE_STATUS).

The classes themselves contain just a simple get/set w/out any extra code to them.

When I try to insert, I get the error stated below.

Thanks in advance!

Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="CMDataWS"
                   assembly="CMDataWS">

  <class name="esm_device_info" table="esm_device_info" lazy="false">

    <id name="DEVICE_SEQ_ID" column="DEVICE_SEQ_ID" type="integer">
      <generator class="sequence">
        <param name="sequence">ESM_DEVICE_INFO_S01</param>
      </generator>
    </id>

    <property name="DEVICE_NAME" column="DEVICE_NAME" type="string"/>
    <property name="DEVICE_TYPE_CODE" column="DEVICE_TYPE_CODE" type="string"/>   
    <property name="DSCR" column="DSCR" type="string"/>
    <property name="ENV_TYPE_CODE" column="ENV_TYPE_CODE" type="string"/>
    <property name="LOCTN_CODE" column="LOCTN_CODE" type="string"/>
    <property name="SSL_GROUP_SEQ_ID" column="SSL_GROUP_SEQ_ID" type="integer"/>
    <property name="OWNER_COMPANY_CODE" column="OWNER_COMPANY_CODE" type="string"/>
    <property name="COST_CENTER_CHARGEBACK" column="COST_CENTER_CHARGEBACK" type="string"/>
    <property name="SERIAL_NBR" column="SERIAL_NBR" type="string"/>
    <property name="APERTURE_ASSET_TAG" column="APERTURE_ASSET_TAG" type="string"/>
    <property name="RACK_ID" column="RACK_ID" type="string"/>
    <property name="CREATE_PROG_NAME" column="CREATE_PROG_NAME" type="string"/>
    <property name="CREATE_USER_ID" column="CREATE_USER_ID" type="string"/>
    <property name="CREATE_DATE" column="CREATE_DATE" type="DateTime"/>
    <property name="CHG_LST_PROG_NAME" column="CHG_LST_PROG_NAME" type="string"/>
    <property name="CHG_LST_USER_ID" column="CHG_LST_USER_ID" type="string"/>
    <property name="CHG_LST_DATE" column="CHG_LST_DATE" type="DateTime"/>
    <property name="DOMAIN_SUFFIX" column="DOMAIN_SUFFIX" type="string"/>

    <bag name="ESM_DEVICE_STATUS_LIST" cascade="all" lazy="false" table="ESM_DEVICE_STATUS">
      <key column="DEVICE_SEQ_ID" />
      <one-to-many class="CMDataWS.esm_device_status, CMDataWS" />
    </bag>

  </class>

</hibernate-mapping>



Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="CMDataWS"
                   assembly="CMDataWS">

  <class name="esm_device_status" table="esm_device_status" lazy="false">

    <composite-id name="ESM_DEVICE_STATUS_PK" >
      <key-property name="DEVICE_SEQ_ID" type="integer" column="DEVICE_SEQ_ID"/>
      <key-property name="DEVICE_STATUS_DATE" type="datetime" column="DEVICE_STATUS_DATE"/>
    </composite-id>
   
    <property name="DEVICE_SEQ_ID" type="integer" column="DEVICE_SEQ_ID"/>
    <property name="DEVICE_STATUS_DATE" column="DEVICE_STATUS_DATE" type="datetime"/>
    <property name="DEVICE_STATUS_CODE" column="DEVICE_STATUS_CODE" type="string"/>
    <property name="CHANGE_REQUEST_NBR" column="CHANGE_REQUEST_NBR" type="string"/>
    <property name="CREATE_PROG_NAME" column="CREATE_PROG_NAME" type="string"/>
    <property name="CREATE_USER_ID" column="CREATE_USER_ID" type="string"/>
    <property name="CREATE_DATE" column="CREATE_DATE" type="DateTime"/>
    <property name="CHG_LST_PROG_NAME" column="CHG_LST_PROG_NAME" type="string"/>
    <property name="CHG_LST_USER_ID" column="CHG_LST_USER_ID" type="string"/>
    <property name="CHG_LST_DATE" column="CHG_LST_DATE" type="DateTime"/>

    <many-to-one name="ESM_DEVICE_INFO" class="CMDataWS.esm_device_info, CMDataWS" column="DEVICE_SEQ_ID"/>
  </class>

</hibernate-mapping>


Code:
{"Invalid index 12 for this OracleParameterCollection with Count=12."}
    [System.IndexOutOfRangeException]: {"Invalid index 12 for this OracleParameterCollection with Count=12."}
    Data: {System.Collections.ListDictionaryInternal}
    HelpLink: null
    InnerException: null
    Message: "Invalid index 12 for this OracleParameterCollection with Count=12."
    Source: "System.Data.OracleClient"
    StackTrace: "   at System.Data.OracleClient.OracleParameterCollection.RangeCheck(Int32 index)\r\n   at System.Data.OracleClient.OracleParameterCollection.GetParameter(Int32 index)\r\n   at System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index)\r\n   at NHibernate.Type.DateTimeType.Set(IDbCommand st, Object value, Int32 index) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Type\\DateTimeType.cs:line 61\r\n   at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Type\\NullableType.cs:line 180\r\n   at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Type\\NullableType.cs:line 139\r\n   at NHibernate.Type.ComponentType.NullSafeSet(IDbCommand st, Object value, Int32 begin, ISessionImplementor session) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Type\\ComponentType.
cs:line 212\r\n   at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Persister\\Entity\\AbstractEntityPersister.cs:line 2361\r\n   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Persister\\Entity\\AbstractEntityPersister.cs:line 2715\r\n   at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernat
e\\Persister\\Entity\\AbstractEntityPersister.cs:line 2627\r\n   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Persister\\Entity\\AbstractEntityPersister.cs:line 2885\r\n   at NHibernate.Action.EntityUpdateAction.Execute() in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Action\\EntityUpdateAction.cs:line 70\r\n   at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Engine\\ActionQueue.cs:line 130\r\n   at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Engine\\ActionQueue.cs:line 113\r\n   at NHibernate.Engine.ActionQueue.ExecuteActions() in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Engine\\ActionQueue.cs:line 147\r\n   at NHibernate.Eve
nt.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Event\\Default\\AbstractFlushingEventListener.cs:line 240\r\n   at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Event\\Default\\DefaultFlushEventListener.cs:line 19\r\n   at NHibernate.Impl.SessionImpl.Flush() in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Impl\\SessionImpl.cs:line 1186\r\n   at CMDataWS.Manager.SaveOrUpdateDeviceInfoRecord(String sXML) in C:\\SRC\\CMDataWS\\CMDataWS\\Manager.cs:line 137"
    TargetSite: {Void RangeCheck(Int32)}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 3:14 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post the code where you create and save the new objects ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 2:52 pm 
Newbie

Joined: Fri Jul 25, 2008 6:35 pm
Posts: 6
I sure can. I'll try to minimize it as much as possible:

Code:
                    oDeviceInfo = (esm_device_info)oSession.Load(typeof(esm_device_info), oRaw.DEVICE_SEQ_ID);
                   
                    esm_device_status oStatus = new oStatus();
                    //... oStatus sets ...
                    oDeviceInfo.ESM_DEVICE_STATUS_LIST.Add(oStatus);

                    //have tried save / update / and SaveOrUpdate
                    oSession.SaveOrUpdate(oDeviceInfo);
                    oSession.Flush();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 4:11 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You have a bidirectional association, so you have to mark the many end as inverse:

Code:
<bag name="ESM_DEVICE_STATUS_LIST" cascade="all" lazy="false" table="ESM_DEVICE_STATUS" inverse="true">
      <key column="DEVICE_SEQ_ID" />
      <one-to-many class="CMDataWS.esm_device_status, CMDataWS" />
    </bag>


Have a look at: http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/example-parentchild.html

And you have to set the parent in the oStatus instance to oDeviceInfo. Hibernate does not do that for you:

Code:
oDeviceInfo.ESM_DEVICE_STATUS_LIST.Add(oStatus);
oStatus.DeviceInfo = oDeviceInfo;


Or better you implement an Add() method on esm_device_info that takes care of that.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 11:50 am 
Newbie

Joined: Fri Jul 25, 2008 6:35 pm
Posts: 6
I've been down that road since I've last posted.

    Added inverse="true" in parent collection declaration
    Parent was set in the child in "//...oStatus sets.." (I apologize for not showing everything)


I get this error when trying to:

Code:
oSession.Save/SaveOrUpdate/Update(oStatus);


Current Error
Code:
{"Unexpected row count: 0; expected: 1"}
    [NHibernate.StaleStateException]: {"Unexpected row count: 0; expected: 1"}
    Data: {System.Collections.ListDictionaryInternal}
    HelpLink: null
    HResult: -2146232832
    InnerException: null
    Message: "Unexpected row count: 0; expected: 1"
    Source: "NHibernate"
    StackTrace: "   at NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, IDbCommand statement) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\AdoNet\\Expectations.cs:line 29\r\n   at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\AdoNet\\NonBatchingBatcher.cs:line 40\r\n   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Persister\\Entity\\AbstractEntityPersister.cs:line 2715\r\n   at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session) in c:\\CSharp\\NH2.0.0\\
nhibernate\\src\\NHibernate\\Persister\\Entity\\AbstractEntityPersister.cs:line 2627\r\n   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Persister\\Entity\\AbstractEntityPersister.cs:line 2885\r\n   at NHibernate.Action.EntityUpdateAction.Execute() in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Action\\EntityUpdateAction.cs:line 70\r\n   at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Engine\\ActionQueue.cs:line 130\r\n   at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Engine\\ActionQueue.cs:line 113\r\n   at NHibernate.Engine.ActionQueue.ExecuteActions() in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Engine\\ActionQueue.cs:line 1
47\r\n   at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Event\\Default\\AbstractFlushingEventListener.cs:line 246\r\n   at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Event\\Default\\DefaultFlushEventListener.cs:line 19\r\n   at NHibernate.Impl.SessionImpl.Flush() in c:\\CSharp\\NH2.0.0\\nhibernate\\src\\NHibernate\\Impl\\SessionImpl.cs:line 1186\r\n   at CMDataWS.Manager.SaveOrUpdateDeviceInfoRecord(String sXML) in C:\\SRC\\CMDataWS\\CMDataWS\\Manager.cs:line 148"
    TargetSite: {Void VerifyOutcomeNonBatched(Int32, System.Data.IDbCommand)}


I appreciate the quick responses. At this point, I've manually written the inserts for that collection -- however, I'd still like to get this working like it's supposed to!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 2:28 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
hmmm, not really sure what's happening here. Have you followed the rules for composite ids:

[url]Your persistent class must override Equals() and GetHashCode() to implement composite identifier equality. It
must also be Serializable.[/url]

from http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html#mapping-declaration-compositeid

Using a component id or a generated id might be a better approach for this.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 10:57 am 
Newbie

Joined: Fri Jul 25, 2008 6:35 pm
Posts: 6
Yeah -- I've override(den) those -- which is why when I query/update, I don't get multiple of the same ones (which is what happens if I use just the seq_id as the id vs the composite).

Yeah -- its a nasty problem!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 11:17 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can try and set unsaved-value="any" on the composite id. I found that in another post. For inserts it should be easy to handle, but I'm not sure about the consequences for updates ...

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 12:28 pm 
Newbie

Joined: Fri Jul 25, 2008 6:35 pm
Posts: 6
Strange..

Seeing this error again.

Code:
{"Invalid index 10 for this OracleParameterCollection with Count=10."}


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