Hello,
I've run into the following problem while deleting child from parent collection. I have two entities parent and childs with one-to-many relation. One-to-many mapping is done with <bag> tag like that :
Code:
<bag name="Lignes" cascade="all-delete-orphan" lazy="false">
<key column="ZL_IDPIECE" />
<one-to-many class="Ligne" />
</bag>
So when I try to remove child from parent collection and than to save parent entity with SaveOrUpdate method the following error occurs "could not delete collection". I noticed that in the log file NHibernate just tries to set null value to my primary key in the child table and than fails which is normal because null values are not authorized.
Than I added inverse="true" into the <bag> tag in the parent mapping file. When I run SaveOrUpdate on parent item after removing some items from child collection the error doesn't occur but ALL is DELETED from the database ! Parent with all childs ! When I looked into my log file I see NHibernate running delete statements on all childs than on parent. Why ? I don't understand what I'm doing wrong. Thanks for your help in advance.
Thomas
Hibernate version: 2.0.0.1001 Mapping documents:Parent
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="CommandesModel"
assembly="CommandesModel">
<!-- Mappings for class 'ZPiece' -->
<class name="Piece" table="ZPIECE" lazy="false">
<!-- Identity mapping -->
<id name="IDPIECE">
<column name="ZP_IDPIECE" />
<generator class="assigned" />
</id>
<version column="ZP_VERSION" name="VERSION" type="Int32" access="property" unsaved-value="0" generated="never" />
<!-- One-to-many mapping: Lignes -->
<bag name="Lignes" cascade="all-delete-orphan" lazy="false" inverse="true">
<key column="ZL_IDPIECE" />
<one-to-many class="Ligne" />
</bag>
<property name="SOUCHE" column="ZP_SOUCHE" />
<property name="REFINTERNE" column="ZP_REFINTERNE" />
<property name="REFEXTERNE" column="ZP_REFEXTERNE" />
</class>
</hibernate-mapping>
Child
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="CommandesModel"
assembly="CommandesModel">
<!-- Mappings for class 'Ligne' -->
<class name="Ligne" table="ZLIGNE" lazy="false">
<!-- Identity mapping -->
<id name="IDLIGNE">
<column name="ZL_IDLIGNE" not-null="true" />
<generator class="assigned" />
</id>
<version column="ZL_VERSION" name="VERSION" type="Int32" access="property" unsaved-value="0" generated="never" />
<many-to-one name="Piece" class="Piece" cascade="all">
<column name="ZL_IDPIECE" not-null="true" />
</many-to-one>
<property name="NATUREPIECEG" column="ZL_NATUREPIECEG" />
<property name="NUMERO" column="ZL_NUMERO" />
<property name="NUMLIGNE" column="ZL_NUMLIGNE" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
public void Save<T>(T item)
{
using (ISession session = m_SessionFactory.OpenSession())
{
using (session.BeginTransaction())
{
session.SaveOrUpdate(item);
session.Transaction.Commit();
}
}
}
Debug level Hibernate log excerpt:
2008-05-07 09:08:14,678 [9] DEBUG NHibernate.SQL [(null)] <(null)> - UPDATE ZLIGNE SET ZL_VERSION = @p0, ZL_IDPIECE = @p1, ZL_NATUREPIECEG = @p2, ZL_NUMERO = @p3, ZL_NUMLIGNE = @p4, ZL_SOUCHE = @p5, ZL_ARTICLE = @p6, ZL_LIBELLE = @p7, ZL_TIERS = @p8, ZL_TIERSFACTURE = @p9, ZL_TIERSLIVRE = @p10, ZL_REPRESENTANT = @p11, ZL_COMMISSIONR = @p12, ZL_TYPECOM = @p13, ZL_PIECEPRECEDENTE = @p14, ZL_CODEARTICLE = @p15, ZL_REFARTSAISIE = @p16, ZL_REFARTTIERS = @p17, ZL_REFARTBARRE = @p18, ZL_TYPEREF = @p19, ZL_PIECEORIGINE = @p20, ZL_DEPOT = @p21, ZL_TYPELIGNE = @p22, ZL_TARIFARTICLE = @p23, ZL_TARIFTIERS = @p24, ZL_TYPEREMISE = @p25, ZL_DEVISE = @p26, ZL_FAMILLETAXE1 = @p27, ZL_ETABLISSEMENT = @p28, ZL_CREATEUR = @p29, ZL_UTILISATEUR = @p30, ZL_REGIMETAXE = @p31, ZL_PAYSORIGINE = @p32, ZL_SOLDERELIQUAT = @p33, ZL_TYPENOMENC = @p34, ZL_TYPEARTICLE = @p35, ZL_QUALIFQTEVTE = @p36, ZL_QUALIFQTESTO = @p37, ZL_FACTUREHT = @p38, ZL_VIVANTE = @p39, ZL_TENUESTOCK = @p40, ZL_ESCOMPTABLE = @p41, ZL_NONIMPRIMABLE = @p42, ZL_REMISABLELIGNE = @p43, ZL_REMISABLEPIED = @p44, ZL_CODEDOUANE = @p45, ZL_CODESDIM = @p46, ZL_CREERPAR = @p47, ZL_SOCIETE = @p48, ZL_CODECOND = @p49, ZL_QUALIFMVT = @p50, ZL_NUMEROSERIE = @p51, ZL_TYPEDIM = @p52, ZL_REGROUPELIGNE = @p53, ZL_USERCHANGEMENT = @p54, ZL_REFEXTERNE = @p55, ZL_GENERAUTO = @p56, ZL_FACTURABLE = @p57, ZL_ETATSOLDE = @p58, ZL_BLOCNOTE = @p59, ZL_UNITEPRIX = @p60, ZL_TAUXDEV = @p61, ZL_COTATION = @p62, ZL_QTEFACT = @p63, ZL_QTETARIF = @p64, ZL_ESCOMPTE = @p65, ZL_TOTALHT = @p66, ZL_TOTALHTDEV = @p67, ZL_MONTANTHT = @p68, ZL_MONTANTHTDEV = @p69, ZL_MONTANTTTC = @p70, ZL_MONTANTTTCDEV = @p71, ZL_TOTALTAXE1 = @p72, ZL_TOTALTAXEDEV1 = @p73, ZL_TOTALTTC = @p74, ZL_TOTALTTCDEV = @p75, ZL_TOTREMPIED = @p76, ZL_TOTREMPIEDDEV = @p77, ZL_REMISEPIED = @p78, ZL_TOTREMLIGNE = @p79, ZL_TOTREMLIGNEDEV = @p80, ZL_REMISELIGNE = @p81, ZL_TOTESCLIGNE = @p82, ZL_TOTESCLIGNEDEV = @p83, ZL_VALEURREMDEV = @p84, ZL_QTERELIQUAT = @p85, ZL_QTERESTE = @p86, ZL_PUHT = @p87, ZL_PUHTDEV = @p88, ZL_PUHTNET = @p89, ZL_PUHTNETDEV = @p90, ZL_PUTTC = @p91, ZL_PUTTCDEV = @p92, ZL_PUTTCNET = @p93, ZL_PUTTCNETDEV = @p94, ZL_PUHTBASE = @p95, ZL_PUTTCBASE = @p96, ZL_PUHTORIGINE = @p97, ZL_PUHTORIGINEDEV = @p98, ZL_PRIXPOURQTE = @p99, ZL_REMISELIBRE = @p100, ZL_DATEPIECE = @p101, ZL_DATELIVRAISON = @p102, ZL_DATEMODIF = @p103, ZL_DATECREATION = @p104, ZL_DATECHANGEMENT = @p105, ZL_NUMORDRE = @p106, ZL_TARIF = @p107, ZL_NUMADRESSELIVR = @p108, ZL_INDICENOMEN = @p109, ZL_INDICELOT = @p110, ZL_INDICESERIE = @p111, ZL_IDENTIFIANTWOL = @p112, ZL_G = @p113, ZL_GPARENTID = @p114, ZL_QTENOM = @p115, ZL_QTEPIERRE = @p116, ZL_QTEPESEEREEL = @p117, ZL_QTEPESEARRONDI = @p118, ZL_QTEG = @p119, ZL_QTERESTEALIVRER = @p120, ZL_QTERETOURNE = @p121, ZL_QTEFACTURE = @p122, ZL_SOLDEAFACTURER = @p123, ZL_LIGNELIEE = @p124, ZL_FACTURE = @p125 WHERE ZL_IDLIGNE = @p126 AND ZL_VERSION = @p127; @p0 = '1', @p1 = 'CC5 ', @p2 = 'CC ', @p3 = '5', @p4 = '1', @p5 = 'GCC', @p6 = 'A10/10 001002 X', @p7 = 'Brillants - Diamètre 2,70 GH VS1', @p8 = 'CAUGM00', @p9 = 'CAUGM00', @p10 = 'CAUGM00', @p11 = '', @p12 = '0,0000', @p13 = ' ', @p14 = '', @p15 = 'A10/10', @p16 = 'A10/10', @p17 = '', @p18 = '', @p19 = 'ART', @p20 = '', @p21 = '100', @p22 = 'ART', @p23 = '002', @p24 = ' ', @p25 = '', @p26 = 'USD', @p27 = 'TN', @p28 = '001', @p29 = 'CEG', @p30 = 'CEG', @p31 = 'FRA', @p32 = '', @p33 = '-', @p34 = ' ', @p35 = 'MAR', @p36 = 'CRT', @p37 = 'CRT', @p38 = 'X', @p39 = 'X', @p40 = 'X', @p41 = 'X', @p42 = '-', @p43 = 'X', @p44 = 'X', @p45 = '', @p46 = '', @p47 = 'IMP', @p48 = '001', @p49 = '', @p50 = '', @p51 = '-', @p52 = 'NOR', @p53 = '', @p54 = '', @p55 = 'CDE MODIF', @p56 = 'MAN', @p57 = 'X', @p58 = 'ENC', @p59 = '', @p60 = 'CRT', @p61 = '0,00', @p62 = '0,666666667', @p63 = '5,0000', @p64 = '0,00', @p65 = '0,00', @p66 = '563,33', @p67 = '845,00', @p68 = '563,33', @p69 = '845,00', @p70 = '673,75', @p71 = '1010,62', @p72 = '110,41', @p73 = '165,62', @p74 = '0,00', @p75 = '0,00', @p76 = '0,00', @p77 = '0,00', @p78 = '0,00', @p79 = '0,00', @p80 = '0,00', @p81 = '0,00', @p82 = '0,00', @p83 = '0,00', @p84 = '0,00', @p85 = '0,8000', @p86 = '4,2000', @p87 = '563,33', @p88 = '845,00', @p89 = '563,33', @p90 = '845,00', @p91 = '673,75', @p92 = '1010,62', @p93 = '449,17', @p94 = '1010,62', @p95 = '0,00', @p96 = '0,00', @p97 = '0,00', @p98 = '0,00', @p99 = '1,00', @p100 = '0,00', @p101 = '07/05/2008 08:49:20', @p102 = '07/05/2008 08:49:20', @p103 = '01/01/1900 00:00:00', @p104 = '07/05/2008 00:00:00', @p105 = '07/05/2008 09:07:56', @p106 = '1', @p107 = '0', @p108 = '1', @p109 = '0', @p110 = '0', @p111 = '0', @p112 = '0', @p113 = '', @p114 = '', @p115 = '1,0000', @p116 = '0,800000000', @p117 = '0,000000000', @p118 = '0,0000', @p119 = '0,0000', @p120 = '0,8000', @p121 = '0,000000000', @p122 = '5,0000', @p123 = '0,000000000', @p124 = '0', @p125 = '-', @p126 = 'CC5-2 ', @p127 = '1'
2008-05-07 09:08:14,810 [9] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] <(null)> - Closed IDbCommand, open IDbCommands: 0
2008-05-07 09:08:14,826 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister [(null)] <(null)> - Deleting entity: [CommandesModel.Ligne#CC5-1 ]
2008-05-07 09:08:14,826 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister [(null)] <(null)> - Version: 1
2008-05-07 09:08:14,826 [9] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] <(null)> - Opened new IDbCommand, open IDbCommands: 1
2008-05-07 09:08:14,826 [9] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] <(null)> - Building an IDbCommand object for the SqlString: DELETE FROM ZLIGNE WHERE ZL_IDLIGNE = ? AND ZL_VERSION = ?
2008-05-07 09:08:14,826 [9] DEBUG NHibernate.Type.StringType [(null)] <(null)> - binding 'CC5-1 ' to parameter: 0
2008-05-07 09:08:14,826 [9] DEBUG NHibernate.Type.Int32Type [(null)] <(null)> - binding '1' to parameter: 1
2008-05-07 09:08:14,826 [9] DEBUG NHibernate.SQL [(null)] <(null)> - DELETE FROM ZLIGNE WHERE ZL_IDLIGNE = @p0 AND ZL_VERSION = @p1; @p0 = 'CC5-1 ', @p1 = '1'
2008-05-07 09:08:14,837 [9] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] <(null)> - Closed IDbCommand, open IDbCommands: 0
2008-05-07 09:08:14,845 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister [(null)] <(null)> - Deleting entity: [CommandesModel.Ligne#CC5-2 ]
2008-05-07 09:08:14,845 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister [(null)] <(null)> - Version: 1
2008-05-07 09:08:14,845 [9] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] <(null)> - Opened new IDbCommand, open IDbCommands: 1
2008-05-07 09:08:14,845 [9] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] <(null)> - Building an IDbCommand object for the SqlString: DELETE FROM ZLIGNE WHERE ZL_IDLIGNE = ? AND ZL_VERSION = ?
2008-05-07 09:08:14,845 [9] DEBUG NHibernate.Type.StringType [(null)] <(null)> - binding 'CC5-2 ' to parameter: 0
2008-05-07 09:08:14,845 [9] DEBUG NHibernate.Type.Int32Type [(null)] <(null)> - binding '1' to parameter: 1
2008-05-07 09:08:14,845 [9] DEBUG NHibernate.SQL [(null)] <(null)> - DELETE FROM ZLIGNE WHERE ZL_IDLIGNE = @p0 AND ZL_VERSION = @p1; @p0 = 'CC5-2 ', @p1 = '1'
2008-05-07 09:08:14,846 [9] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] <(null)> - Closed IDbCommand, open IDbCommands: 0
2008-05-07 09:08:14,847 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister [(null)] <(null)> - Deleting entity: [CommandesModel.Piece#CC5 ]
2008-05-07 09:08:14,847 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister [(null)] <(null)> - Version: 1
2008-05-07 09:08:14,847 [9] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] <(null)> - Opened new IDbCommand, open IDbCommands: 1
2008-05-07 09:08:14,847 [9] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] <(null)> - Building an IDbCommand object for the SqlString: DELETE FROM ZPIECE WHERE ZP_IDPIECE = ? AND ZP_VERSION = ?
2008-05-07 09:08:14,847 [9] DEBUG NHibernate.Type.StringType [(null)] <(null)> - binding 'CC5 ' to parameter: 0
2008-05-07 09:08:14,847 [9] DEBUG NHibernate.Type.Int32Type [(null)] <(null)> - binding '1' to parameter: 1
2008-05-07 09:08:14,847 [9] DEBUG NHibernate.SQL [(null)] <(null)> - DELETE FROM ZPIECE WHERE ZP_IDPIECE = @p0 AND ZP_VERSION = @p1; @p0 = 'CC5 ', @p1 = '1'
2008-05-07 09:08:14,853 [9] DEBUG NHibernate.AdoNet.AbstractBatcher [(null)] <(null)> - Closed IDbCommand, open IDbCommands: 0
Name and version of the database you are using: SQL Server 2005