Nhibernate 3.2按代码映射。当我们创建一个包关系时,什么意思rm.OneToMany?

时间:2012-01-23 20:32:11

标签: c# nhibernate nhibernate-mapping

我通过代码使用nhibernate 3.2映射,我有一个奇怪的行为。

如果我简化我的项目,我有2个表:

[Serializable]
public class Intervention
{
    public Intervention()
    {
        ReponsePointVerification = new List<ReponsePointVerification>();
    }

    #region Public Properties
    public virtual int Id
    {
        get;
        set;
    }
    public virtual IList<ReponsePointVerification> ReponsePointVerification
    {
        get;
        set;
    }
}

public class InterventionMap : ClassMapping<Intervention>
{
    public InterventionMap()
    {
        Id<int>(x => x.Id, map =>
        {
            map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
        });
        Bag(x => x.ReponsePointVerification, map =>
        {
            map.Key( k => k.Column( "IdIntervention" ) );
        }); 
    }
}

[Serializable]
public class ReponsePointVerification
{
    #region Public Properties
    public virtual int Id
    {
        get;
        set;
    }
    public virtual Intervention Intervention
    {
        get;
        set;
    }
}

public class ReponsePointVerificationMap : ClassMapping<ReponsePointVerification>
{
    public ReponsePointVerificationMap()
    {
        Id<int>(x => x.Id, map =>
        {
            map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
        });
        ManyToOne<Intervention>(x => x.Intervention, map => map.Column("IdIntervention"));
    }
}

代表两个表Intervention和ReponsePointVerification,它有一个与干预相关联的外键(IdIntervention)。

我打电话的时候:

        return (from interv in Session.Query<Intervention>()
                .Fetch(rep => rep.ReponsePointVerification)
                select interv).ToList();

甚至在我不提取信息时。我有这个错误:

Could not cast the value in field id0_ of type Int32 to the Type SerializableType.  Please check to make sure that the mapping is correct and that your DataProvider supports this Data Type.

sql查询看起来没问题。

我发现帖子http://groups.google.com/group/nhusers/browse_thread/thread/ef137c3e5b66acdc

我根据该帖子修改了InterventionMap类:

public class InterventionMap : ClassMapping<Intervention>
{
    public InterventionMap()
    {
        Id<int>(x => x.Id, map =>
        {
            map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
        });
        Bag(x => x.ReponsePointVerification, map =>
        {
            map.Key( k => k.Column( "IdIntervention" ) );
        }, rm => rm.OneToMany()); 
    }
}

唯一的区别是添加“,rm =&gt; rm.OneToMany()”

相信我,我很高兴它有效,但我真的不明白那条线能否有人解释我的意思?

此致

修改

我做了一个WriteAllXmlMapping,两个代码是:

版本没有rm =&gt; rm.OneToMany()(不起作用)

<bag name="ReponsePointVerification">
  <key column="IdIntervention" />
  <element type="Hyma.BusinessObjets.ReponsePointVerification" />
</bag>
带有rm =&gt;的

版本rm.OneToMany()(工作)

<bag name="ReponsePointVerification">
  <key column="IdIntervention" />
  <one-to-many class="ReponsePointVerification" />
</bag>

1 个答案:

答案 0 :(得分:3)

OneToMany告诉NH这是与另一个映射实体的一对多关系。如果留空,则NH将ElementCollection作为默认值,这是与某个没有ID的ElementType的一对多关系。

例如,当你有这些类

class Parent
{
    public ICollection<Child> Childs { get; set; }  <- one-to-many collection
    public ICollection<string> Names { get; set; }  <- element collection
}

class Child
{
    public int Id { get; set; }
    public string Name { get; set; }
}
然后NH会创建这3个表

ParentTable
id

ChildTable
id | parent_id | name

NamesTable
parent_id | name

反之亦然,当你有3个表时,将它映射到类

是合理的