Dear Nhibernate-user
I am trying to obtain a one-to-one relation.
A MabcCode contains the properties: Code, Description, Uom, UomFactor, RemarkCostprices. Above that it has an element OverridenCostprice. This Overriden Costprice with its properties for a certain Mabc-Item can be found in table TBL_OVERRIDEN_COSTPRICES with its Price and Currency. Field OvCos_Mabc_Code in TBL_OVERRIDEN_COSTPRICES has a foreign key towards field Mabc_Code in TBL_MABCCODES, which has a primary key. I use a separate table for these Overriden Costprices, because every property of it should be filled in: if the Overriden Costprice of a certain Mabc-Item can be found then it should be complete OR the Overriden Costprice does not exist at all for the given Mabc-item.
Code:
CREATE TABLE [dbo].[TBL_OVERRIDEN_COSTPRICES](
[OvCos_Mabc_Code] [nvarchar](5) NOT NULL,
[OvCos_Price] [decimal](18, 3) NOT NULL,
[OvCos_Cur_Iso] [nvarchar](3) NOT NULL,
CREATE TABLE [dbo].[TBL_MABCCODES](
[Mabc_Code] [nvarchar](5) NOT NULL,
[Mabc_Description] [nvarchar](50) NULL,
[Mabc_Uom_Id] [int] NOT NULL,
[Mabc_UomFactor] [decimal](18, 3) NOT NULL,
[Mabc_RemarkCostprices] [nvarchar](500) NOT NULL,
Here are my mapping files:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernateProject.bsMabc, NHibernateProject" table="TBL_MABCCODES">
<id name="Code" column="Mabc_Code" type="String" length="5">
<generator class="assigned"/>
</id>
<property name="Description" column="Mabc_Description" type="String" length="50"/>
<many-to-one name="Uom" column="Mabc_Uom_Id" class="NHibernateProject.bsUom, NHibernateProject"/>
<property name="UomFactor" column="Mabc_UomFactor" type="Decimal"/>
<property name="RemarkCostprices" column="Mabc_RemarkCostprices" type="String" length="50"/>
<one-to-one name="OverridenCostprice" class="NHibernateProject.bsOverridenCostprice, NHibernateProject"/>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernateProject.bsOverridenCostprice, NHibernateProject" table="TBL_OVERRIDEN_COSTPRICES">
<id name="MabcCode" column="OvCos_Mabc_Code" type="String" length="5">
<generator class="assigned"/>
</id>
<property name="Price" column="OvCos_Price" type="Decimal"/>
<many-to-one name="Currency" column="OvCos_Cur_Iso" class="NHibernateProject.bsCurrency, NHibernateProject"/>
</class>
</hibernate-mapping>
When I try the following code for creating a new Mabc-item, the Mabc-item is saved in the database, but NOT its Overriden Costprice (as the Mabc-item’s member) in the other table. Is their something missing in my Mapping file and/or can I obtain exactly the same result in another way, maybe more simple? I can save my Overriden Costprice-object separately, but Nhibernate should be able to save a Mabc-item as a whole including its Overriden Costprice, isn’t it?
Code:
Dim myConfiguration As Configuration = New Configuration
myConfiguration.AddAssembly("NHibernateProject")
Dim factory As ISessionFactory = myConfiguration.BuildSessionFactory
Dim session As ISession = factory.OpenSession
Dim transaction As ITransaction = session.BeginTransaction
Dim myMabc As New bsMabc
myMabc.Code = "ZZZZZ"
myMabc.Description = "My Mabc-item"
myMabc.Uom = Uoms.GetItem(3)
myMabc.UomFactor = CDec(2.345)
myMabc.RemarkCostprices = "Overriden costprice needs to be reviewed!!"
Dim myOverridenCostprice As New bsOverridenCostprice
myOverridenCostprice.MabcCode = myMabc.Code
myOverridenCostprice.Currency = Currencies.GetItem("USD")
myOverridenCostprice.Price = CDec(23.234)
myMabc.OverridenCostprice = myOverridenCostprice
session.Save(myMabc)
transaction.Commit()
MessageBox.Show("ok")
I highly appreciate your reply on this
Bennieke