Hi folks,
I have a problem with a one-to-many relationship :
I have 2 classes Transactions and TransationLines
Code:
public class TransactionInfo
{
public TransactionInfo()
{
Lines = new ArrayList();
}
public Int32 Id;
public string CommentTop;
public string CommentBottom;
...
[XmlArray("Lines"), XmlArrayItem(typeof(TransactionLineInfo))]
public IList Lines;
}
public class TransactionLineInfo
{
public TransactionLineInfo()
{
}
public Int32 Id;
public int Quantite;
public string Description;
...
}
mapped as :
Code:
<class
name="MyCompany.Modules.ModA.Models.Transactions.TransactionInfo, MyCompany.Modules.ModA.Models"
table="Transactions_Transactions">
<id name="Id" column="TransactionId" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="CommentTop" type="String" />
<property name="CommentBottom" type="String" />
....
<bag name="Lines" table="TransactionLignes" cascade="all-delete-orphan" lazy="false" inverse="true">
<key column="TransactionId" />
<one-to-many class="MyCompany.Modules.ModA.Models.Transactions.TransactionLineInfo, MyCompany.Modules.ModA.Models" />
</bag>
</class>
<class
name="MyCompany.Modules.ModA.Models.Transactions.TransactionLineInfo, MyCompany.Modules.ModA.Models"
table="Transactions_TransactionLignes">
<id name="Id" column="TransactionLigneId" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Quantite" type="Int32" />
<property name="Description" type="String" />
...
<!-- Note: the 'transactionId' column that is used to make the link between a Transaction and its lines is not mapped -->
</class>
Let's take an example :
Code:
Transaction trx = new Transaction();
TransactionLine l1 = new TransactionLine();
trx.CommentTop = "....";
l1.Description = ".....";
trx.Lines.Add(l1);
session.Save(trx);
The problem is that 'trx' is saved, 'l1' is saved but the link between trx and l1 is not assigned in the database (ie table TransactionLine.TransactionId = null)
I checked the generated sql and saw that at no time nhibernate does the association between trx and l1.
Here is the sequence in the log:
- INSERT INTO Transaction ... trx's properties ....
- Get the new Trx'Id ("Natively generated identity...")
- Detects the cascades
- INSERT INTO TransactionLines ... l1's properties BUT NO REFERENCE TO TRANSACTIONID !! ....
I checked with and without "inverse=true", giving the same result.
Someone faced the same problem ?
Thx in advance and have a nice day.