Fluent NHibernate PersistenceSpecification not-null属性引用null或transient值错误

时间:2013-07-23 22:10:04

标签: unit-testing fluent-nhibernate persistence

我几天来一直在努力解决这个烂摊子,试图弄清楚错误的确切原因。一句话,失败!基本上我一直试图对我用ClassMap引用另一个reference的{​​{1}}进行单元测试。

reference

您可以清楚地看到我的 [TestMethod] public void IssueReturnRegisterMap_Create_Success() { var maxDifference = TimeSpan.FromMilliseconds(990); BookSize sz = new BookSize() { ID = "1", Name = "A" }; Department dpt = new Department() { ID = "1", Name = "Philosophy" }; Author auth = new Author() { ID = "2", FirstName = "Wise", LastName = "Person" }; Publisher pub = new Publisher() { ID = "1", Name = "Pub1", Address = "Address 1" }; Language lang = new Language() { ID = "1", Name = "Lang1" }; Patron ptrn = new Patron() { ID = "1", FirstName = "First", LastName = "Last", Age = 82, Address = "Address1", Gender = "Male", Occupation = "Occupation1", Telephone1 = "01234567890", Telephone2 = "01234567890" }; Book book = new Book() { ID = "1", Name = "Book1", Number = "12", BookSize = sz, Department = dpt, Author = auth, Commentator = "Another wiseman", Publisher = pub, Language = lang, }; Member member = new Member() { ID = "1", Patron = ptrn, IsRegistered = true }; //Edit: Acc. to Firo's suggestion Database.Session.Save( sz ); Database.Session.Save( dpt ); Database.Session.Save( auth ); Database.Session.Save( lang ); Database.Session.Save( pub ); Database.Session.Save( ptrn ); Database.Session.Save( book ); //@Firo: Throws same exception here! :-( Database.Session.Save( member ); new PersistenceSpecification<IssueReturnEntry>( Database.Session, new CustomEqualityComparer() ) .CheckProperty( x => x.ID, "1" ) .CheckReference( x=> x.Book, book ) .CheckReference( x => x.Member, member ) .CheckProperty( x => x.IssuedBy, "Librarian" ) .CheckProperty( x => x.IssueDate, DateTime.Now, new DateTimeEqualityComparer( maxDifference ) ) .CheckProperty( x => x.DueDate, DateTime.Now, new DateTimeEqualityComparer( maxDifference ) ) .CheckProperty( x => x.ReturnDate, DateTime.Now, new DateTimeEqualityComparer( maxDifference ) ) .CheckProperty( x => x.ReturnBy, "User" ) .VerifyTheMappings(); } Book referencesBookSize等组成。然后Department本身变为Book 1}}用于references地图。同样适用于IssueReturnRegister。它抛出一个&not-null-null属性引用null或transient值错误&#39; Member对象的异常。任何帮助是极大的赞赏。提前谢谢。

3 个答案:

答案 0 :(得分:0)

错误意味着Booksize由要存储的实体引用但本身未存储且无法存储在此操作中,因为级联已禁用。

所以要么

    映射中的
  • References(x => x.BookSize).Cascade.All()
  • 在运行Database.Session.Save(bz) 之前
  • PersistenceSpecification

所有其他引用实体

相同

更新:持久性实体应该有一个Equals方法将id考虑在内

public override bool Equals(object obj)
{
    var other = obj as BookSize;
    return other != null && (Id == 0) ? ReferenceEquals(this, other) : Id == other.Id;
}

private int? _hashcode;
public override int GetHashCode()
{
    return _hashcode.HasValue ? _hashcode.Value : _hashcode = Id;
}

答案 1 :(得分:0)

对不起Firo,我认为我的回复不够可读,因此再次在此发布。

if (x is BookSize && y is BookSize)
{ 
return (((BookSize)x).ID == ((BookSize)y).ID) && (((BookSize)x).Name == ((BookSize)y).Name) && (((BookSize)x).Length == ((BookSize)y).Length) && (((BookSize)x).Width == ((BookSize)y).Width);
}

这是我为EqualityComparer获得的equals方法。我还没有任何GetHashCode方法。

答案 2 :(得分:0)

我的BookMapBookSizeMap如下所示,然后使用Book实体对PersistenceSpecification实体进行单元测试,其中没有任何此类异常(请注意,我已经尝试过了)根据{{​​1}}实体的单元测试执行,但不适用于Book实体。

IssueRegisterEntry