0

I'm trying to save an one-to-many mapping and nHibernate throws me this error because its assign the same ID for the two entries of the ordPsiPt table.

I looked around and using the Session.merge() did not work either.

Here is the mapping of the classes:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
  <class name="ordPsiPt" table="ord_psi_pt" lazy="true" >
    <id name="idOrdPastaIT">
        <generator class="identity" />
    </id>
    <many-to-one lazy="false" name="ordPsi">
      <column name="idOrdPastaI" sql-type="int(11)" not-null="true" />
    </many-to-one>
    <many-to-one lazy="false" name="pastaTopping">
      <column name="idPastaT" sql-type="int(11)" not-null="true" />
    </many-to-one>
    <property name="amount">
      <column name="Amount" sql-type="varchar(50)" not-null="true" />
    </property>
  </class>
</hibernate-mapping>

order_psi:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
  <class name="ordPsi" table="ord_psi" lazy="true" >
    <id name="idOrdPastaI">
      <generator class="identity" />
    </id>
    <many-to-one lazy="false" name="order">
      <column name="idOrder" sql-type="int(11)" not-null="true" />
    </many-to-one>
    <many-to-one lazy="false" name="pastaIndividual" class="pastaIndividual">
      <column name="idPastaI" sql-type="int(11)" not-null="false" />
    </many-to-one>
    <property name="obs">
      <column name="obs" sql-type="varchar(50)" not-null="false" />
    </property>
    <property name="price">
      <column name="price" sql-type="decimal(8,4)" not-null="true" />
    </property>

    <bag name="ordPsiPt" table="ordPsiPt" cascade="save-update" inverse="true">
      <key column="idOrdPastaIT"/>
      <one-to-many class="ordPsiPt"/>
    </bag>

  </class>
</hibernate-mapping>

order:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
  <class name="order" table="`order`" lazy="true" >
    <id name="idOrder">
      <generator class="identity" />
    </id>
    <property name="price">
      <column name="price" sql-type="decimal(8,4)" not-null="true" />
    </property>
    <property name="cash">
      <column name="cash" sql-type="tinyint(1)" not-null="false" />
    </property>
    <property name="credit">
      <column name="credit" sql-type="tinyint(1)" not-null="false" />
    </property>
    <property name="obs">
      <column name="obs" sql-type="varchar(350)" not-null="false" />
    </property>

    <bag name="orderPsi" table="ordPsi" cascade="all" inverse="true">
      <key column="idOrdPastaI"/>
      <one-to-many class="ordPsi"/>
    </bag>

  </class>
</hibernate-mapping>

The bag takes IList on the cs mapping.

When I call SaveOrUpdate() with the order object, having two orderPsiPt inside one orderPsi (just for example) I got the error about the identifiers.

I tried to use conn.Session.Merge(order.orderPsi[0].ordPsiPt[1]); just for this sample but no sucess also.

If that helps, here are the watches from the two ordPsiPt itens: http://db.tt/2wNWldyd

I've also tried to look at this:

http://ayende.com/blog/4282/nhibernate-cross-session-operations

Fenton
  • 241,084
  • 71
  • 387
  • 401
gbc921
  • 316
  • 2
  • 6
  • 22

1 Answers1

0

Never mind. I found that my database was wrong with the mapping. I had updated the mapping and forget to edit the real table.

The table ordPsiPt was like that:

Old Table

And should be like that, to match my new mapping as stated above:

New table

After fixing the table on the database, everything went fine on the first shot!

gbc921
  • 316
  • 2
  • 6
  • 22