Nhibernate 3.2代码映射.当我们创建一个袋子关系时,什么是rm.一对多

本文关键字:关系 一个 一对多 rm 什么 映射 代码 我们 创建 Nhibernate | 更新日期: 2023-09-27 18:21:06

我通过代码使用nhibernate 3.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,其具有链接到Intervention的外键(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=>rm.OneToMany()"

相信我,我很高兴它成功了,但我真的不明白这句话,有人能解释我的意思吗?

问候

编辑

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

不带rm=>rm的版本。OneToMany()(无效)

<bag name="ReponsePointVerification">
  <key column="IdIntervention" />
  <element type="Hyma.BusinessObjets.ReponsePointVerification" />
</bag>

带有rm=>rm的版本。OneToMany()(有效)

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

Nhibernate 3.2代码映射.当我们创建一个袋子关系时,什么是rm.一对多

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个表时,将其映射到类

是合理的