-->
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: Master/Detail error
PostPosted: Mon Sep 26, 2005 3:36 pm 
Newbie

Joined: Mon Sep 26, 2005 3:26 pm
Posts: 6
I'm to save a master/detail records in HR schema of the oracle database, but i have this error:
"SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0). Possible causes: the row was modified or deleted by another user, or a trigger is reporting misleading row count."

my full code here:

Code:
         Configuration cfg = new Configuration();
         cfg.AddClass(typeof(NHDomainModel.Region));
         cfg.AddClass(typeof(NHDomainModel.Country));

         ISessionFactory factory = cfg.BuildSessionFactory();
         ISession session = factory.OpenSession();
         
         ITransaction tran = session.BeginTransaction();

         NHDomainModel.Region myRegion = new NHDomainModel.Region();
         myRegion.RegionName = "Latin America";

         NHDomainModel.Country myCountry = new NHDomainModel.Country();
         myCountry.CountryName = "Panama City";
         myCountry.CountryId = "PM";
         myCountry.Region = myRegion;

         myRegion.Countries.Add(myCountry);

         session.Save(myRegion);
         session.Flush();

         tran.Commit();
         session.Close();


xml mapping:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHDomainModel.Region, NHDomainModel"  table="REGIONS">
    <id name="RegionId" column="REGION_ID" type="Int32" unsaved-value="0" >
       <generator  class="sequence">
          <param name="sequence">REGION_SEQ</param>
       </generator>
    </id>
    <property name="RegionName" column="REGION_NAME" type="String"/>
    <set name="Countries" cascade="all" lazy="false" inverse="true">
      <key column="REGION_ID" />
      <one-to-many class="NHDomainModel.Country, NHDomainModel" />
    </set>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHDomainModel.Country, NHDomainModel"  table="COUNTRIES">
    <id name="CountryId" column="COUNTRY_ID" unsaved-value="" >
       <generator  class="assigned" />
    </id>
    <property name="CountryName" column="COUNTRY_NAME" />    
    <many-to-one name="Region" column="REGION_ID" class="NHDomainModel.Region, NHDomainModel" cascade="none" not-null="true" />
</class>
</hibernate-mapping>


thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 4:26 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
The problem is assigned identifiers - the country has an identifier "PM" which is different from the unsaved value (""), so NHibernate thinks you are updating an existing record and executes UPDATE instead of INSERT.


Top
 Profile  
 
 Post subject: error persists
PostPosted: Mon Sep 26, 2005 5:46 pm 
Newbie

Joined: Mon Sep 26, 2005 3:26 pm
Posts: 6
I delete attribute unsave-value but the mistake persists and I replace set by bag tag in region xml mapping file.

my new hbm.xml files are:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHDomainModel.Region, NHDomainModel"  table="REGIONS">
    <id name="RegionId" column="REGION_ID" type="Int32" unsaved-value="0" >
       <generator  class="sequence">
          <param name="sequence">REGION_SEQ</param>
       </generator>
    </id>
    <property name="RegionName" column="REGION_NAME" type="String"/>
    <bag name="Countries" cascade="all" lazy="false" inverse="true">
      <key column="REGION_ID" />
      <one-to-many class="NHDomainModel.Country, NHDomainModel" />
    </bag>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHDomainModel.Country, NHDomainModel"  table="COUNTRIES">
    <id name="CountryId" column="COUNTRY_ID">
       <generator  class="assigned" />
    </id>
    <property name="CountryName" column="COUNTRY_NAME" />     
    <many-to-one name="Region" column="REGION_ID" class="NHDomainModel.Region, NHDomainModel" />
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 7:22 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Well, no matter what unsaved-value is, the problem will still be there, since NHibernate cannot determine from the identifier alone whether it needs to execute INSERT or UPDATE. If possible, add a <version> property to the class (and a corresponding column to the table). Another more complicated option is writing your own IInterceptor implementation which will be able to determine whether the object exists or not.


Top
 Profile  
 
 Post subject: version use
PostPosted: Tue Sep 27, 2005 3:49 pm 
Newbie

Joined: Mon Sep 26, 2005 3:26 pm
Posts: 6
you give a example of the version attribute use.
In the table add a column version (NUMBER)
In the class Country add a property Version (int)
.... ???

thanks


Top
 Profile  
 
 Post subject: Re: version use
PostPosted: Tue Sep 27, 2005 4:31 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
ErickGB wrote:
you give a example of the version attribute use.
In the table add a column version (NUMBER)
In the class Country add a property Version (int)
.... ???


Then you map it using <version name="Version" column="version" unsaved-value="0" />.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 5:40 pm 
Newbie

Joined: Mon Sep 26, 2005 3:26 pm
Posts: 6
I think that was mistaken in the syntax, but it was not this

A new error is:

Additional Information: The element 'urn:nhibernate-mapping-2.0:id' it a secundary element 'urn:nhibernate-mapping-2.0:version' no valid. Error in , (6, 5).

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHDomainModel.Country, NHDomainModel"  table="COUNTRIES">
    <id name="CountryId" column="COUNTRY_ID">
       <generator  class="assigned" />
    </id>
    <property name="CountryName" column="COUNTRY_NAME" />   
   <version name="Version" column="version" unsaved-value="0" />
    <many-to-one name="Region" column="REGION_ID" class="NHDomainModel.Region, NHDomainModel" />
</class>
</hibernate-mapping>


thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 1:44 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Move <version /> before <property />.
(The schema required xml elements to be ordered in some places...)

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject: Not save Detail
PostPosted: Fri Sep 30, 2005 11:02 am 
Newbie

Joined: Mon Sep 26, 2005 3:26 pm
Posts: 6
The master data is saved in database, but not the detail data.
thanks


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.