wolli wrote:
The only thing I can see, that's different from the documentation and the mappings that I use, is the missing cascade on the collection. Try to add cascade="all" to the set mapping and see if that helps.
This was just a typo in my post. I had the cascade in the mapping.
After digging a little deeper I have figured out exactly what is happening but I am unsure how to fix it.
My problem is with mapping an inheritance Child to a Parent. Looking at the log files I see that the Parent/Child mapping as described in Chapter 17 is working properly. I am getting the two insert statements and no update statement now.
I have the following classes:
Code:
public class Visit
{
public int ID { get; set; }
public IList<Visitor> Visitors { get; set; }
public Requestor Requestor { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
public class Person
{
public String Name { get; set; }
}
public class Requestor : Person
{
public String Section { get; set; }
public IList<Visit> Visits { get; set; }
}
public class Visitor : Person
{
// Some properties
}
They are mapped as follows:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Visit, Examples.Visit" table="visit">
<id name="ID" column="pk_id" type="int">
<generator class="native" />
</id>
<many-to-one name="Requestor" column="requestor_id" not-null="false" />
</class>
<class name="Person, Examples.Person" table="person">
<id name="ID" column="pk_id" type="int">
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="name" length="50" sql-type="varchar" not-null="false" />
</property>
<joined-subclass name="Requestor, Examples.Requestor" table="requestor" lazy="false">
<key column="fk_person_id" />
<bag name="Visits" table="visit" cascade="all-delete-orphan" inverse="true" lazy="true">
<key column="fk_requestor_id" />
<one-to-many class="Visit" />
</bag>
<property name="Section" type="String" not-null="true">
<column name="section" length="50" sql-type="varchar" not-null="true" />
</property>
</joined-subclass>
</class>
</hibernate-mapping>
Code:
CREATE TABLE visit (
pk_id int NOT NULL,
Name nvarchar(40) default NOT NULL,
fk_requestor_id int NULL
PRIMARY KEY (pk_id)
)
CREATE TABLE person (
name varchar(50) NOT NULL,
)
CREATE TABLE requestor (
pk_id int NOT NULL,
fk_person_id int NOT NULL
)
When I execute the save on Requestor I get the following inserts based on the log file:
INSERT INTO person (Name) VALUES (@p0)
after the above statement I see a log statement
Select SCOPE_IDENTITY();
returning '40' as column:
NHibernate.Persister.Entity.AbstractEntityPersister [(null)] - Nativley generate identity: 40
This is the pk_id generate for the newly inserted person.
and then I see the following insert:
INSERT INTO requestor (fk_person_id) VALUES (@p0)
where @p0 is the identity previously Selected of 40.
Finally, I see
INSERT INTO visit (Name, fk_requestor_id) VALUES (@p0, @p1)
where @p0 is the value in the Visit.Name Property and
@p1 is 40.
This final 40 is where my problem exists. It is trying to insert the visit using the person pk_id and not the requestor pk_id. In fact there is no Select SCOPE_IDENTITY(); called on the requestor table after the insert of the requestor data.
Thanks ahead of time for any help.