Reputation: 144
Class:
[DataContract]
public class Parent
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
[DataMember]
public virtual IList<string> Values { get; set; }
public Parent()
{
}
}
ORM mapping file:
<class name="Testing.Models.Parent" table="Parent" >
<id name="Id">
<generator class="native" />
</id>
<property name="Name" />
<list name="Values" table="ChildValue" inverse="true" >
<key column="ParentId" />
<index column="Sequence" type="System.Int32"/>
<element column="Value" type="System.String"/>
</list>
</class>
But while testing with save of parent entity, it only inserts into parent table. ChildValue table is not inserted with any record, though I provided the Values .
I found somewhere inverse has to be true, even with that it is not working.
[update: insert code] Create method
public IList<Parent> Create(IList<Parent> entities)
{
using (ISession session = SessionFactory.OpenSession())
{
foreach (var entity in entities)
{
session.Save(entity);
}
return entities;
}
}
ISessionFactory is injected using Unity and configured as
UnityContainer.RegisterInstance(typeof(ISessionFactory),
new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory());
While nhibernate configuration is like(in app.config)
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2012Dialect,NHibernate</property>
<property name="connection.connection_string_name">db</property>
<!-- set show_sql property to false on production-->
<property name="show_sql">true</property>
<mapping assembly="Testing"/>
</session-factory>
</hibernate-configuration>
Test method which creates entities is
[TestMethod]
public void TestCreate()
{
IList<Parent> entities = new List<Parent>();
for (int index = 0; index < 5; index++)
{
var entity = TestHelper.CreateEntity<Parent>();
entity.Values = new List<string>();
entity.Values.Add("Values" + index);
entities.Add(entity);
}
entities = instance.Create(entities);
VerifyPickList(entities, "Create");
Assert.AreEqual(5, (int)TestHelper.Execute("SELECT COUNT(*) FROM" +
" [dbo].[Parent]"), "Create is wrong.");
}
[update 2: sql]
-- parent table
CREATE TABLE [dbo].[Parent](
[Id] [int] IDENTITY(1,1) NOT NULL PRimary Key,
[Name] [varchar](45) NOT NULL,
)
-- child table
CREATE TABLE [dbo].[ChildValue](
[ParentId] [int] NOT NULL,
[Value] [varchar](45) NOT NULL,
[Sequence] [int] NOT NULL,
CONSTRAINT [PK__Parent__D099C8FD03317E3D] PRIMARY KEY CLUSTERED
(
[ParentId] ASC,
[Value] ASC
)
ALTER TABLE [dbo].[ChildValue] WITH CHECK ADD CONSTRAINT [pk_plv_pl] FOREIGN KEY([ParentId])
REFERENCES [dbo].[Parent] ([Id])
GO
ALTER TABLE [dbo].[ChildValue] CHECK CONSTRAINT [pk_plv_pl]
GO
Upvotes: 1
Views: 660
Reputation: 123861
The most important here is to NOT use inverse="true"
.
<list name="Values" table="ChildValue"
// inverse="true" is NO OPTION for element lists
inverse="false" // inverse="false" or nothing
>
<key column="ParentId" />
<index column="Sequence" type="System.Int32"/>
<element column="Value" type="System.String"/>
</list>
The point is, that NHibernate takes this setting as: "The other end will care about persistence..." which is ok, if there is a class, the <one-to-many>
. But not ok, if there is <element>
Class (mapped as one-to-many
) is first level citizen, which can care about persistence... string cannot
This code should now do INSERT into both tables:
var parent = Parent { Name = "abc" };
parent.Values = new List<string> { "testValue" };
session.Save(parent);
Check the doc, e.g.
with an example:
<set name="Aliases" table="person_aliases" sort="natural">
<key column="person"/>
<element column="name" type="String"/>
</set>
Check this article for more details:
Upvotes: 1