-->
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.  [ 8 posts ] 
Author Message
 Post subject: Add or Update a child row problem
PostPosted: Thu Jun 29, 2006 10:43 pm 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
I'm trying to use MySQL for my database backend. Now I got
Code:
TestCase 'Talisay.Commands.Tests.CreateTest.TestCreateCustomer'
failed: NHibernate.ADOException : could not insert: [Talisay.Core.Customer]
  ----> MySql.Data.MySqlClient.MySqlException : #23000Cannot add or update a child row: a foreign key constraint fails (`talisay/customers`, CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`Id`) REFERENCES `salesorders` (`CustomerId`))
   at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)
   at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session)
   at NHibernate.Impl.ScheduledIdentityInsertion.Execute()
   at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
   at NHibernate.Impl.SessionImpl.DoSave(Object theObj, Key key, IClassPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
   at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IClassPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
   at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
   at NHibernate.Impl.SessionImpl.Save(Object obj)
   D:\ian\projects\csharp\talisay\src\Talisay\Commands\Create.cs(31,0): at Talisay.Commands.Create.Execute()
   D:\ian\projects\csharp\talisay\src\Talisay\Commands\Tests\CreateTest.cs(26,0): at Talisay.Commands.Tests.CreateTest.TestCreateCustomer()
   --ADOException
   at MySql.Data.MySqlClient.PacketReader.CheckForError()
   at MySql.Data.MySqlClient.PacketReader.ReadHeader()
   at MySql.Data.MySqlClient.PacketReader.OpenPacket()
   at MySql.Data.MySqlClient.NativeDriver.ReadResult(Int64& affectedRows, Int64& lastInsertId)
   at MySql.Data.MySqlClient.CommandResult.ReadNextResult(Boolean isFirst)
   at MySql.Data.MySqlClient.NativeDriver.SendQuery(Byte[] bytes, Int32 length, Boolean consume)
   at MySql.Data.MySqlClient.MySqlCommand.GetNextResultSet(MySqlDataReader reader)
   at MySql.Data.MySqlClient.MySqlCommand.Consume()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at NHibernate.Impl.BatcherImpl.ExecuteNonQuery(IDbCommand cmd)
   at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)

TestFixture failed:


0 passed, 1 failed, 0 skipped, took 4.30 seconds.

Here's my Customers table
Code:
CREATE TABLE `customers` (
  `Id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`Id`),
  CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`Id`) REFERENCES `salesorders` (`CustomerId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC

And the mapping files
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="Talisay.Core" assembly="Talisay">
   <class name="Customer" table="Customers">
      <id name="Id" column="Id" type="Int32">
         <generator class="identity"/>
      </id>
      <property name="CustomerName" column="CustomerName"/>
      <property name="Title" column="Title"/>
      <component name="Name" class="Name">
         <property name="LastName" column="LastName"/>
         <property name="FirstName" column="FirstName"/>
         <property name="MiddleName" column="MiddleName"/>
      </component>
      <bag name="Orders" cascade="all" access="nosetter.camelcase" lazy="false" inverse="true">
         <key column="Id"/>
         <one-to-many class="SalesOrder"/>
      </bag>
   </class>
</hibernate-mapping>

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="Talisay.Core" assembly="Talisay">
   <class name="SalesOrder" table="SalesOrders">
      <id name="Number" column="Number">
         <generator class="identity"/>
      </id>
      <many-to-one name="Customer" column="CustomerId" class="Customer" cascade="none" />
   </class>
</hibernate-mapping>

Any hint? I'm kinda lost. Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 11:14 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
your orders collection is referencing the wrong foreignkey. should be:

Code:
      <bag name="Orders" cascade="all" access="nosetter.camelcase" lazy="false" inverse="true">
         <key column="CustomerID"/>
         <one-to-many class="SalesOrder"/>
      </bag>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 11:37 pm 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
Still I got the error. :(

I even rename the Customers.Id to Customers.CustomerId for less confusion. But still got the

Code:
TestCase 'Talisay.Commands.Tests.CreateTest.TestCreateCustomer'
failed: NHibernate.ADOException : could not insert: [Talisay.Core.Customer]
  ----> MySql.Data.MySqlClient.MySqlException : #23000Cannot add or update a child row: a foreign key constraint fails (`talisay/customers`, CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`CustomerId`) REFERENCES `salesorders` (`CustomerId`))
   at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)
   at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session)
   at NHibernate.Impl.ScheduledIdentityInsertion.Execute()
   at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
   at NHibernate.Impl.SessionImpl.DoSave(Object theObj, Key key, IClassPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
   at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IClassPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
   at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
   at NHibernate.Impl.SessionImpl.Save(Object obj)
   d:\ian\projects\csharp\talisay\src\talisay\commands\create.cs(31,0): at Talisay.Commands.Create.Execute()
   d:\ian\projects\csharp\talisay\src\talisay\commands\tests\createtest.cs(26,0): at Talisay.Commands.Tests.CreateTest.TestCreateCustomer()
   --ADOException
   at MySql.Data.MySqlClient.PacketReader.CheckForError()
   at MySql.Data.MySqlClient.PacketReader.ReadHeader()
   at MySql.Data.MySqlClient.PacketReader.OpenPacket()
   at MySql.Data.MySqlClient.NativeDriver.ReadResult(Int64& affectedRows, Int64& lastInsertId)
   at MySql.Data.MySqlClient.CommandResult.ReadNextResult(Boolean isFirst)
   at MySql.Data.MySqlClient.NativeDriver.SendQuery(Byte[] bytes, Int32 length, Boolean consume)
   at MySql.Data.MySqlClient.MySqlCommand.GetNextResultSet(MySqlDataReader reader)
   at MySql.Data.MySqlClient.MySqlCommand.Consume()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at NHibernate.Impl.BatcherImpl.ExecuteNonQuery(IDbCommand cmd)
   at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)

TestFixture failed:


0 passed, 1 failed, 0 skipped, took 3.63 seconds.


The mapping files now are
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="Talisay.Core" assembly="Talisay">
   <class name="Customer" table="Customers">
      <id name="Id" column="CustomerId" type="Int32">
         <generator class="identity"/>
      </id>
      <property name="CustomerName" column="CustomerName" type="String"/>
      <property name="Title" column="Title" type="String"/>
      <component name="Name" class="Name">
         <property name="LastName" column="LastName" type="String"/>
         <property name="FirstName" column="FirstName" type="String"/>
         <property name="MiddleName" column="MiddleName" type="String"/>
      </component>
      <bag name="Orders" cascade="all" access="nosetter.camelcase" lazy="false" inverse="true">
         <key column="CustomerId"/>
         <one-to-many class="SalesOrder"/>
      </bag>
   </class>
</hibernate-mapping>


and

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="Talisay.Core" assembly="Talisay">
   <class name="SalesOrder" table="SalesOrders">
      <id name="Number" column="Number" type="Int32">
         <generator class="identity"/>
      </id>
      <many-to-one name="Customer" column="CustomerId" class="Customer" cascade="none" />
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 12:56 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
please post the code you are using to create and save the object. sometimes it can be tricky when you are creating a new SalesOrder and a new Customer and cascading the save through the Customer object.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 1:06 am 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
This is my test

Code:
[Test] public void TestCreateCustomer()
      {
         Customer c = new Customer();
         c.CustomerName = "Flat Foot Software";
         c.Name = new Name("Escarro", "Ian", "Carilimdiliman");
         new Create(c).Execute();
      }


The Create command

Code:
/*
* Create.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;
using NHibernate;
using NHibernate.Cfg;
using Talisay.Utilities;

namespace Talisay.Commands
{
   /// <summary>
   /// Summary description for Create.
   /// </summary>
   public class Create : AbstractCommand
   {
      private object obj;

      public Create(object obj)
      {
         this.obj = obj;
      }

      public override void Execute()
      {
         ISession session = ConfigurationSingleton.Instance.OpenSession();
         ITransaction trans = session.BeginTransaction();
         session.Save(obj);
         trans.Commit();
         session.Close();
      }
   }
}


The ConfigurationSingleton

Code:
/*
* ConfigurationSingleton.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;
using NHibernate;
using NHibernate.Cfg;

namespace Talisay.Utilities
{
   /// <summary>
   /// Summary description for ConfigurationSingleton.
   /// </summary>
   public class ConfigurationSingleton
   {
      private Configuration conf = new Configuration();
      private ISessionFactory factory;

      private static ConfigurationSingleton instance = new ConfigurationSingleton();
      public static ConfigurationSingleton Instance
      {
         get { return instance; }
      }

      private ConfigurationSingleton()
      {
         conf.AddAssembly("Talisay");
         factory = conf.BuildSessionFactory();
      }

      public ISession OpenSession()
      {
         return factory.OpenSession();
      }
   }
}


Thanks for the patience.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 1:24 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
no worries. i'm actually a bit stumped. if you are creating the Customer without adding a SalesOrder, then your call to save Customer should not generate a cascade to the SalesOrder collection. can you post your code for the Customer class? also, as a test, comment out the bag in your Customer.hbm.xml and see if your test fails. i'm going to go to bed now but i'll check in the morning to see if you have answered.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 1:30 am 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
Again thanks. This is my Customer class

Code:
/*
* Customer.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;
using System.Collections;

namespace Talisay.Core
{
   /// <summary>
   /// Summary description for Customer.
   /// </summary>
   public class Customer
   {
      private int id;
      private string customerName;
      private string companyName;
      private string title;
      private Name name = new Name();
      private Address address = new Address();
      private string phone;
      private string fax;
      private string email;
      private ContactProfile contact = new ContactProfile();
      private IList orders = new ArrayList();

      public Customer() {}

      public int Id
      {
         get { return id; }
         set { id = value; }
      }

      public string CustomerName
      {
         get { return customerName; }
         set { customerName = value; }
      }

      public string CompanyName
      {
         get { return companyName; }
         set { companyName = value; }
      }

      public string Title
      {
         get { return title; }
         set { title = value; }
      }

      public Name Name
      {
         get { return name; }
         set { name = value; }
      }

      public Address Address
      {
         get { return address; }
         set { address = value; }
      }

      public string Phone
      {
         get { return phone; }
         set { phone = value; }
      }

      public string Fax
      {
         get { return fax; }
         set { fax = value; }
      }

      public string Email
      {
         get { return email; }
         set { email = value; }
      }

      public ContactProfile Contact
      {
         get { return contact; }
         set { contact = value; }
      }

      public IList Orders
      {
         get { return orders; }
      }
   }
}


And my SalesOrder class

Code:
/*
* SalesOrder.cs 0.1
*
* Copyright 2006 Ian Escarro. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

using System;
using System.Collections;

namespace Talisay.Core
{
   /// <summary>
   /// Summary description for SalesOrder.
   /// </summary>
   public class SalesOrder
   {
      private int number;
      private DateTime date;
      private Customer customer;
      private IList items = new ArrayList();

      public SalesOrder() {}

      public int Number
      {
         get { return number; }
         set { number = value; }
      }

      public DateTime Date
      {
         get { return date; }
         set { date = value; }
      }

      public Customer Customer
      {
         get { return customer; }
         set { customer = value; }
      }

      public IList Items
      {
         get { return items; }
      }
   }
}


Also, I got the same error with I comment out <bag>. My test

Code:
[Test] public void TestCreateCustomer()
      {
         Customer c = new Customer();
         c.CustomerName = "Flat Foot Software";
         c.Name = new Name("Escarro", "Ian", "Carilimdiliman");
         new Create(c).Execute();
      }


and my Customer.hbm

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="Talisay.Core" assembly="Talisay">
   <class name="Customer" table="Customers">
      <id name="Id" column="CustomerId" type="Int32">
         <generator class="identity"/>
      </id>
      <property name="CustomerName" column="CustomerName" type="String"/>
      <property name="Title" column="Title" type="String"/>
      <component name="Name" class="Name">
         <property name="LastName" column="LastName" type="String"/>
         <property name="FirstName" column="FirstName" type="String"/>
         <property name="MiddleName" column="MiddleName" type="String"/>
      </component>
      <!--<bag name="Orders" cascade="all" access="nosetter.camelcase" lazy="false" inverse="true">
         <key column="CustomerId"/>
         <one-to-many class="SalesOrder"/>
      </bag>-->
   </class>
</hibernate-mapping>


Again, thanks a bunch!

[EDIT]
I guess it has something to do with my table creation. Or I could be terribly wrong. Btw, good night.

:(


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 12:56 am 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
It doesn't seem to have an error if I delete constraint though. Kinda confused!


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