Hi,
how can one get nhibernate to create the index number when using list on
save?
here is a sample code for this.
sql:CREATE TABLE Person(
[Id] UNIQUEIDENTIFIER NOT NULL,
[Name] NVARCHAR(35) NOT NULL,
CONSTRAINT [Person_Pk] PRIMARY KEY CLUSTERED([Id])
)
CREATE TABLE Skill(
[Id] UNIQUEIDENTIFIER NOT NULL,
[Name] NVARCHAR(35) NOT NULL,
[IndexNumber] INT NOT NULL,
[PersonId] UNIQUEIDENTIFIER NOT NULL,
CONSTRAINT [Skill_Pk] PRIMARY KEY CLUSTERED([Id]),
CONSTRAINT [Person_Skill_Fk] FOREIGN KEY ([PersonId]) REFERENCES
[Person]([Id]),
CONSTRAINT [Skill_Ak1] UNIQUE NONCLUSTERED([PersonId], [IndexNumber]))
domain entities and test save
public class Person
{
public Person()
{
Skills = new List<Skill>();
}
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<Skill> Skills { get; protected set; }
}
public class Skill
{
protected Skill()
{
}
public Skill(Person person)
:this()
{
Person = person;
}
public Guid Id { get; set; }
public string Name{ get; set; }
public int Index { get; protected set; }
public Person Person { get; protected set; }
}
public class CreateObjectTests
{
public void Test()
{
Configuration configuration = new
Configuration().Configure(typeof(Person).Assembly,
"NhibernateListSample.Cfg.xml");
var sessionFactory = configuration.BuildSessionFactory();
var session = sessionFactory.OpenSession();
var person = new Person {Id = new
Guid("2a1977a8-815a-4e53-a36e-0a5381487ea3"), Name = "Test User 1"};
person.Skills.Add(new Skill(person){Id = new
Guid("b42c3007-213f-4169-b686-fd1b4629f787"), Name = "Skill 1"});
person.Skills.Add(new Skill(person){Id = new
Guid("1c198810-672d-4843-bf18-b47762f3c473"), Name = "skill 2"});
session.Save(person);
}
}
mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
<class name="Person" lazy="false" table="Person">
<id name="Id" column="Id"
unsaved-value="00000000-0000-0000-0000-000000000000" type="guid">
<generator class="guid.comb" />
</id>
<property name="Name" column="Name" not-null="true" length="35" />
<list name="Skills" cascade="all" batch-size="10" inverse="true"
lazy="true">
<key column="PersonId" />
<list-index column="IndexNumber" base="1" />
<one-to-many class="Skill" />
</list>
</class>
<class name="Skill" lazy="false" table="Skill">
<id name="Id" column="Id"
unsaved-value="00000000-0000-0000-0000-000000000000" type="guid">
<generator class="guid.comb" />
</id>
<property name="Name" column="Name" not-null="true" length="35" />
<property name="Index" column="IndexNumber" insert="false"
update="false" />
<many-to-one name="Person" class="Person" cascade="none"
lazy="false" />
</class>
</hibernate-mapping>
Med Venlig Hilsen / Best Regards
Peter Lehmann
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.