タイトルにあるように、親と子の両方がデータベースに挿入されていますが、リンクされていません。
マッピングリクエスト
<class name="Request" table="Requests">
<id name="RequestId">
<generator class="native" />
</id>
<many-to-one name="BulkReport" class="BulkReport" column="BulkId" cascade="none" lazy="proxy" />
<property name="RunNumber" />
<property name="MobileNumber" />
<property name="Message" />
<property name="RequestedOn" />
<property name="Closed" />
<many-to-one name="Communication" class="Communication" column="CommId" cascade="save-update" lazy="proxy" />
<many-to-one name="Outcome" class="Outcome" column="OutcomeId" cascade="none" lazy="proxy" />
<many-to-one name="Discount" class="Discount" column="Discount" not-found="ignore" cascade="save-update" lazy="proxy" />
</class>
マッピング割引
<class name="Discount" table="Discounts" discriminator-value="BASE">
<id name="DiscountId">
<generator class="native" />
</id>
<discriminator column="DiscountType" not-null="true" />
<property name="MobileNumber" />
<property name="CustomerName" />
<property name="AddedOn" />
<property name="Status" />
<property name="Pending" />
<many-to-one name="Product" class="DiscountProduct" column="DiscountProduct" cascade="none" lazy="proxy" />
<many-to-one name="AddedBy" class="User" cascade="none" lazy="proxy" />
<subclass name="StaffDiscount" discriminator-value="STAFF">
<many-to-one name="Employee" class="Employee" column="EmployeeNumber" cascade="none" lazy="proxy" not-null="false" />
</subclass>
</class>
テストからのSQL
NHibernate: INSERT INTO Discounts (MobileNumber, CustomerName, AddedOn, Status, Pending, DiscountProduct, AddedBy, DiscountType) VALUES (?, ?, ?, ?, ?, ?, ?, 'BASE');@p0 = 700000000, @p1 = 'Test', @p2 = 25/06/2013 14:38:43, @p3 = True, @p4 = False, @p5 = 1, @p6 = 'Test'
NHibernate: select @@identity
NHibernate: INSERT INTO Requests (BulkId, RunNumber, MobileNumber, Message, RequestedOn, Closed, CommId, OutcomeId, Discount) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);@p0 = 1, @p1 = 100, @p2 = 700000000, @p3 = 'Test Request', @p4 = 25/06/2013 14:38:43, @p5 = True, @p6 = NULL, @p7 = 1, @p8 = 0
NHibernate: select @@identity
なぜこれが起こっているのですか?これで完全なブロックを取得しましたか?
編集:要求された情報
割引クラス
public class Discount
{
public virtual int DiscountId { get; set; }
public virtual double MobileNumber { get; set; }
public virtual string CustomerName { get; set; }
public virtual DateTime AddedOn { get; set; }
public virtual bool Status { get; set; }
public virtual bool Pending { get; set; }
public virtual DiscountProduct Product { get; set; }
public virtual User AddedBy { get; set; }
public override string ToString()
{
return "0" + MobileNumber + ", " + CustomerName.Trim();
}
}
リクエストクラス
public class Request
{
public virtual int RequestId { get; set; }
public virtual BulkReport BulkReport { get; set; }
public virtual int RunNumber { get; set; }
public virtual double MobileNumber { get; set; }
public virtual string Message { get; set; }
public virtual DateTime RequestedOn { get; set; }
public virtual Outcome Outcome { get; set; }
public virtual bool Closed { get; set; }
public virtual Communication Communication { get; set; }
public virtual Discount Discount { get; set; }
}
実行したテスト
[Test]
public void Can_add_new_request_with_discount()
{
ISession session = _sessionFactory.OpenSession();
BulkReport report = session.Get<BulkReport>(1);
Outcome outcome = session.Get<Outcome>(1);
User user = session.Get<User>("Test");
DiscountProduct discountProduct = session.Get<DiscountProduct>(1);
Request request = new Request()
{
Message = "Test Request",
BulkReport = report,
Closed = true,
MobileNumber = 07000000000,
RequestedOn = DateTime.Now,
RunNumber = 100,
Outcome = outcome
};
Discount discount = new Discount
{
AddedBy = user,
AddedOn = DateTime.Now,
CustomerName = "Test",
MobileNumber = 07000000000,
Pending = false,
Status = true,
Product = discountProduct
};
request.Discount = discount;
session.Save(request);
var fromDbRequest = session.Get<Request>(request.RequestId);
var fromDbDiscount = session.Get<Discount>(discount.DiscountId);
Assert.IsNotNull(fromDbRequest);
Assert.IsNotNull(fromDbDiscount);
Assert.AreEqual(request.MobileNumber, fromDbRequest.MobileNumber);
Assert.AreEqual(discount.MobileNumber, fromDbDiscount.MobileNumber);
session.Close();
}