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.  [ 1 post ] 
Author Message
 Post subject: many-to-one cascade attribute problem
PostPosted: Sat Apr 07, 2007 3:27 pm 
Regular
Regular

Joined: Mon Oct 02, 2006 12:03 pm
Posts: 62
Hello All,
I'm developing a customer class with Bank_Address association. I've mapped it as many-to-one association with unique="true" attribute:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Model.Customer, ComponentsBroker" table="CUSTOMER">
      <id name="Id" column="ID" type="String">
         <generator class="assigned" />
      </id>
      <component class="Model.Name, ComponentsBroker" name="Name">
         <property name="First_name" column="FIRST_NAME" type="String"/>
         <property name="Second_name" column="SECOND_NAME" type="String"/>
      </component>
      <many-to-one name="Bank_address" class="Model.BankAddress, ComponentsBroker" unique="true" cascade="save-update" column="BANK_ADDRESS"/>
   </class>
</hibernate-mapping>


and I've added cascade="save-update" attribute in order to forget to save Bank_Address previsoly than customer. So, my saveCustomer function is as follow:


Code:
      public void save(Model.Customer obj)
      {
         try
         {
            NHibernate.ITransaction transaction = this.session.BeginTransaction();
            //this.session.Save(obj.Bank_address);
            this.session.Save(obj);
            transaction.Commit();
         }
         catch (Exception ex)
         {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.InnerException.Message);
         }
      }


Instead of,

Code:
      public void save(Model.Customer obj)
      {
         try
         {
            NHibernate.ITransaction transaction = this.session.BeginTransaction();
            this.session.Save(obj.Bank_address);
            this.session.Save(obj);
            transaction.Commit();
         }
         catch (Exception ex)
         {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.InnerException.Message);
         }
      }


So, When I execute this code:

Code:
         Model.Broker broker = new Model.Broker();

         Model.Customer customer = new Model.Customer("2", new Model.Name("Jordi", "Cabré Roca"));
         customer.Bank_address = new Model.BankAddress("2", "15", "La Parra", "Riudecols", "Tarragona", "La Caixa", "435354");

         broker.save(customer);
         broker.delete(customer.Id);


NHibernate says me it:

Quote:
NHibernate.ADOException: could not insert: [Model.Customer#2][SQL: INSERT INTO CUSTOMER (FIRST_NAME, SECOND_NAME, BANK_ADDRESS, ID) VALUES (?, ?, ?, ?)] ---> System.Data.SqlClient.SqlException: Instrucción INSERT en conflicto con la restricción COLUMN FOREIGN KEY 'FK__CUSTOMER__BANK_A__0E391C95'. El conflicto ha aparecido en la base de datos 'KoetDB', tabla 'BANK_ADDRESS', column 'ADDRESS_ID'.


In other works, Bank_Address doesn't exist!!! What Do I do wrong?
Thanks for all.

Address mappings -->
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Model.Address, ComponentsBroker" table="ADDRESS">
      <id name="Id" column="ID" type="String">
         <generator class="assigned" />
      </id>
      <property name="Street" type="String" column="STREET"/>
      <property name="Number_street" type="String" column="NUMBER_STREET"/>
      <property name="City" type="String" column="CITY"/>
      <property name="State" type="String" column="STATE"/>
      
      <joined-subclass name="Model.BankAddress, ComponentsBroker" table="BANK_ADDRESS">
         <key column="ADDRESS_ID"/>
         <property name="BankName" type="String" column="BANK_NAME"/>
         <property name="Account" type="String" column="ACCOUNT"/>
      </joined-subclass>
      
      <joined-subclass name="Model.ContactAddress, ComponentsBroker" table="CONTACT_ADDRESS">
         <key column="ADDRESS_ID"/>
         
         <component class="Model.Name, ComponentsBroker" name="ContactName">
            <property name="First_name" column="FIRST_NAME" type="String"/>
            <property name="Second_name" column="SECOND_NAME" type="String"/>
         </component>
         <property name="Phone" type="String" column="PHONE"/>
      </joined-subclass>
   </class>
</hibernate-mapping>


I add this NHibernate log text-->
Quote:
2007-04-08 11:40:19,939 [516] DEBUG NHibernate.Impl.SessionFactoryImpl - Instantiated session factory
2007-04-08 11:40:19,959 [516] DEBUG NHibernate.Impl.SessionImpl - opened session
2007-04-08 11:40:19,969 [516] DEBUG NHibernate.Transaction.AdoTransaction - begin
2007-04-08 11:40:19,969 [516] DEBUG NHibernate.Connection.DriverConnectionProvider - Obtaining IDbConnection from Driver
2007-04-08 11:40:22,132 [516] DEBUG NHibernate.Impl.SessionImpl - generated identifier: 2
2007-04-08 11:40:22,152 [516] DEBUG NHibernate.Impl.SessionImpl - saving [Model.Customer#2]
2007-04-08 11:40:22,212 [516] DEBUG NHibernate.Engine.Cascades - processing cascades for: Model.Customer
2007-04-08 11:40:22,252 [516] DEBUG NHibernate.Engine.Cascades - cascading to SaveOrUpdate()
2007-04-08 11:40:22,252 [516] DEBUG NHibernate.Engine.Cascades - unsaved-value strategy NULL
2007-04-08 11:40:22,252 [516] DEBUG NHibernate.Impl.SessionImpl - SaveOrUpdate() previously saved instance with id: 2
2007-04-08 11:40:22,252 [516] DEBUG NHibernate.Impl.SessionImpl - updating [Model.BankAddress#2]
2007-04-08 11:40:22,273 [516] DEBUG NHibernate.Engine.Cascades - done processing cascades for: Model.Customer
2007-04-08 11:40:22,273 [516] DEBUG NHibernate.Engine.Cascades - processing cascades for: Model.Customer
2007-04-08 11:40:22,273 [516] DEBUG NHibernate.Engine.Cascades - done processing cascades for: Model.Customer
2007-04-08 11:40:23,044 [516] DEBUG NHibernate.Transaction.AdoTransaction - commit
2007-04-08 11:40:23,044 [516] DEBUG NHibernate.Impl.SessionImpl - flushing session
2007-04-08 11:40:23,044 [516] DEBUG NHibernate.Engine.Cascades - processing cascades for: Model.Customer
2007-04-08 11:40:23,044 [516] DEBUG NHibernate.Engine.Cascades - cascading to SaveOrUpdate()
2007-04-08 11:40:23,044 [516] DEBUG NHibernate.Impl.SessionImpl - SaveOrUpdate() persistent instance
2007-04-08 11:40:23,054 [516] DEBUG NHibernate.Engine.Cascades - done processing cascades for: Model.Customer
2007-04-08 11:40:23,054 [516] DEBUG NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
2007-04-08 11:40:23,054 [516] DEBUG NHibernate.Impl.SessionImpl - Updating entity: [Model.BankAddress#2]
2007-04-08 11:40:23,064 [516] DEBUG NHibernate.Impl.SessionImpl - Processing unreferenced collections
2007-04-08 11:40:23,064 [516] DEBUG NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
2007-04-08 11:40:23,064 [516] DEBUG NHibernate.Impl.SessionImpl - Flushed: 1 insertions, 1 updates, 0 deletions to 2 objects
2007-04-08 11:40:23,064 [516] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2007-04-08 11:40:23,064 [516] DEBUG NHibernate.Impl.Printer - listing entities:
2007-04-08 11:40:23,064 [516] DEBUG NHibernate.Impl.Printer - Model.Customer{Id=2, Bank_address=BankAddress#2, Name=Name{First_name=Jordi, Second_name=Cabré Roca}}
2007-04-08 11:40:23,064 [516] DEBUG NHibernate.Impl.Printer - Model.BankAddress{Id=2, Account=435354, Street=La Parra, Number_street=15, City=Riudecols, State=Tarragona, BankName=La Caixa}
2007-04-08 11:40:23,064 [516] DEBUG NHibernate.Impl.SessionImpl - executing flush
2007-04-08 11:40:23,064 [516] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
2007-04-08 11:40:23,074 [516] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Inserting entity: [Model.Customer#2]
2007-04-08 11:40:23,084 [516] DEBUG NHibernate.Impl.BatcherImpl - Opened new IDbCommand, open IDbCommands: 1
2007-04-08 11:40:23,084 [516] DEBUG NHibernate.Impl.BatcherImpl - Building an IDbCommand object for the SqlString: INSERT INTO CUSTOMER (FIRST_NAME, SECOND_NAME, BANK_ADDRESS, ID) VALUES (?, ?, ?, ?)
2007-04-08 11:40:23,084 [516] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Dehydrating entity: [Model.Customer#2]
2007-04-08 11:40:23,094 [516] DEBUG NHibernate.Type.StringType - binding 'Jordi' to parameter: 0
2007-04-08 11:40:23,094 [516] DEBUG NHibernate.Type.StringType - binding 'Cabré Roca' to parameter: 1
2007-04-08 11:40:23,094 [516] DEBUG NHibernate.Type.StringType - binding '2' to parameter: 2
2007-04-08 11:40:23,094 [516] DEBUG NHibernate.Type.StringType - binding '2' to parameter: 3
2007-04-08 11:40:23,094 [516] DEBUG NHibernate.SQL - INSERT INTO CUSTOMER (FIRST_NAME, SECOND_NAME, BANK_ADDRESS, ID) VALUES (@p0, @p1, @p2, @p3); @p0 = 'Jordi', @p1 = 'Cabré Roca', @p2 = '2', @p3 = '2'
2007-04-08 11:40:25,047 [516] DEBUG NHibernate.Impl.BatcherImpl - Closed IDbCommand, open IDbCommands: 0
2007-04-08 11:40:25,057 [516] DEBUG NHibernate.Util.ADOExceptionReporter - could not insert: [Model.Customer#2] [INSERT INTO CUSTOMER (FIRST_NAME, SECOND_NAME, BANK_ADDRESS, ID) VALUES (?, ?, ?, ?)]
System.Data.SqlClient.SqlException: Instrucción INSERT en conflicto con la restricción COLUMN FOREIGN KEY 'FK__CUSTOMER__BANK_A__0E391C95'. El conflicto ha aparecido en la base de datos 'KoetDB', tabla 'BANK_ADDRESS', column 'ADDRESS_ID'.
Se terminó la instrucción.


Why not Nhibernate insert firstly a bank_address and later customer?


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

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.