流利NHibernate的复合材料到映射类

流利NHibernate的复合材料到映射类

本文介绍了流利NHibernate的复合材料到映射类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何使用CompositeId来映射另一个类。这里是一个测试用例:

表:

  TestParent:
TestParentId(PK)
FavoriteColor

TestChild:
TestParentId(PK)
ChildName(PK)
年龄


$ b

在C#中的类:

  public class TestParent 
{
public TestParent()
{
TestChildList = new List< TestChild>();
}

public virtual int TestParentId {get;组; }
public virtual string FavoriteColor {get;组; }
公共虚拟IList< TestChild> TestChildList {get;组; }
}

public class TestChild
{
public virtual TestParent Parent {get;组; }
public virtual string ChildName {get;组; }
public virtual int Age {get;组; }
$ b $ public override int GetHashCode()
{
return Parent.GetHashCode()^ ChildName.GetHashCode();

$ b public override bool Equals(object obj)
{
if(obj is TestChild)
{
var toCompare = obj as TestChild;
return this.GetHashCode()!= toCompare.GetHashCode();
}
返回false;






Fluent NHibernate映射:

  public class TestParentMap:ClassMap< TestParent> 
{
public TestParentMap()
{
表(TestParent);
Id(x => x.TestParentId).Column(TestParentId)。GeneratedBy.Native();
Map(x => x.FavoriteColor);

HasMany(x => x.TestChildList).KeyColumn(TestParentId)。Inverse()。Cascade.None();



public class TestChildMap:ClassMap< TestChild>
{
public TestChildMap()
{
Table(TestChild);
CompositeId()
.KeyProperty(x => x.ChildName,ChildName)
.KeyReference(x => x.Parent,TestParentId);

Map(x => x.Age);
引用(x => x.Parent,TestParentId); / **中断插入** /
}
}

当我尝试添加新记录,我得到这个错误:

我知道这个错误是由于在CompositeId和References调用中映射了TestParentId列。但是,在查询基于TestParentId的TestChild时,移除References调用会导致另一个错误。



以下是执行查询的代码:

  var session = _sessionBuilder.GetSession(); 
using(var tx = session.BeginTransaction())
{
//创建父项
var p = new TestParent(){FavoriteColor =Red};
session.Save(p);

//创建子
var c = new TestChild()
{
ChildName =First child,
Parent = p,
年龄= 4
};
session.Save(c); //在TestChildMap中引用调用中断

tx.Commit();


//在TestChildMap中没有引用调用的断点
var children = _sessionBuilder.GetSession()。CreateCriteria< TestChild>()
.CreateAlias(Parent ),p)
.Add(Restrictions.Eq(p.TestParentId,1))
.List< TestChild>();

有关如何为此场景创建组合键的想法?我找到了一个更好的解决方案,可以查询和插入。关键是更新TestChild的地图不插入记录。新的映射是:

  public class TestChildMap:ClassMap< TestChild> 
{
public TestChildMap()
{
Table(TestChild);
CompositeId()
.KeyProperty(x => x.ChildName,ChildName)
.KeyReference(x => x.Parent,TestParentId);

Map(x => x.Age);
引用(x => x.Parent,TestParentId)
.Not.Insert(); //插入
}
}


将避免索引超出范围错误

I'm trying to figure out how to use CompositeId to map another class. Here's a test case:

The tables:

TestParent:
  TestParentId (PK)
  FavoriteColor

TestChild:
  TestParentId (PK)
  ChildName (PK)
  Age

The classes in C#:

public class TestParent
{
    public TestParent()
    {
        TestChildList = new List<TestChild>();
    }

    public virtual int TestParentId { get; set; }
    public virtual string FavoriteColor { get; set; }
    public virtual IList<TestChild> TestChildList { get; set; }
}

public class TestChild
{
    public virtual TestParent Parent { get; set; }
    public virtual string ChildName { get; set; }
    public virtual int Age { get; set; }

    public override int GetHashCode()
    {
        return Parent.GetHashCode() ^ ChildName.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        if (obj is TestChild)
        {
            var toCompare = obj as TestChild;
            return this.GetHashCode() != toCompare.GetHashCode();
        }
        return false;
    }
}

The Fluent NHibernate maps:

public class TestParentMap : ClassMap<TestParent>
{
    public TestParentMap()
    {
        Table("TestParent");
        Id(x => x.TestParentId).Column("TestParentId").GeneratedBy.Native();
        Map(x => x.FavoriteColor);

        HasMany(x => x.TestChildList).KeyColumn("TestParentId").Inverse().Cascade.None();
    }
}

public class TestChildMap : ClassMap<TestChild>
{
    public TestChildMap()
    {
        Table("TestChild");
        CompositeId()
            .KeyProperty(x => x.ChildName, "ChildName")
            .KeyReference(x => x.Parent, "TestParentId");

        Map(x => x.Age);
        References(x => x.Parent, "TestParentId");  /**  breaks insert **/
    }
}

When I try to add a new record, I get this error:

I know this error is due to the TestParentId column being mapped in the CompositeId and References calls. However, removing the References call causes another error when querying TestChild based on the TestParentId.

Here's the code that does the queries:

var session = _sessionBuilder.GetSession();
using (var tx = session.BeginTransaction())
{
    // create parent
    var p = new TestParent() { FavoriteColor = "Red" };
    session.Save(p);

    // creat child
    var c = new TestChild()
                {
                    ChildName = "First child",
                    Parent = p,
                    Age = 4
                };
    session.Save(c);  // breaks with References call in TestChildMap

    tx.Commit();
}

// breaks without the References call in TestChildMap
var children = _sessionBuilder.GetSession().CreateCriteria<TestChild>()
    .CreateAlias("Parent", "p")
    .Add(Restrictions.Eq("p.TestParentId", 1))
    .List<TestChild>();

Any ideas on how to create a composite key for this scenario?

解决方案

I found a better solution that will allow querying and inserting. The key is updating the map for TestChild to not insert records. The new map is:

public class TestChildMap : ClassMap<TestChild>
{
    public TestChildMap()
    {
        Table("TestChild");
        CompositeId()
            .KeyProperty(x => x.ChildName, "ChildName")
            .KeyReference(x => x.Parent, "TestParentId");

        Map(x => x.Age);
        References(x => x.Parent, "TestParentId")
            .Not.Insert();  //  will avoid "Index was out of range" error on insert
    }
}

这篇关于流利NHibernate的复合材料到映射类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:51