由于标题声明父和子都被插入数据库但没有被链接。
映射请求
<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 From Test
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();
}
答案 0 :(得分:1)
该测试看起来不对。你真的应该使用交易(并调用transaction.Commit()
)。或者,致电session.Flush()
。如果您这样做,您很可能会获得一个更新语句,用于设置Discount
中的Request
。
与您的问题无关,但您应该打开一个新会话来获取fromDb
个对象,否则不会发出数据库请求,因为它们已经在会话的第一级缓存中