NHibernate级联未设置外键
本文关键字:设置 级联 NHibernate | 更新日期: 2023-09-27 18:19:53
正如标题所述,父级和子级都被插入到数据库中,但没有被链接。
映射请求
<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; }
}
测试I运行
[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();
}
那个测试看起来不对。您确实应该使用事务(并调用transaction.Commit()
)。或者,呼叫session.Flush()
。如果执行任一操作,则很可能会得到一个update语句,该语句将Discount
设置在Request
中。
与您的问题无关,但您应该打开一个新的会话来获取fromDb
对象,否则不会发出DB请求,因为它们已经在会话的一级缓存中