简体   繁体   English

Fluent Nhibernate 聚合对象映射

[英]Fluent Nhibernate Aggregate objects mapping

Say I have the following class with an aggregation of an external class:假设我有以下 class 与外部 class 的聚合:

public class MyMovie
{
    public virtual string id{get;set;}
    public virtual Movie movie{get;set;}
}

//These classes are externally defined and cannot be changed.
public class Movie
{
    public string title{get;set;}
    public IList<Director> Directors{get;set;}
}

public class Director
{
    public string name{get;set;}
    public IList<Movie> DirectedMovies{get;set;}
}

The db schema for this would be three tables:用于此的 db 架构将是三个表:

Movie(m_id, title)电影(m_id,标题)

Director(d_id, name)导演(d_id,姓名)

Directs(m_id, d_id)指导(m_id,d_id)

Is it possible to map this with fluent nhibernate?是否可以通过流畅的 nhibernate 实现 map? I just don't understand how this would be done with the many to many relation being in the external classes where I cannot map create a map class for Director as this does not define members as virtual.我只是不明白如何在外部类中的多对多关系中完成此操作,在该类中我无法为 Director 创建 map class,因为这并未将成员定义为虚拟。

Basically, your issue here is that you are trying to tell nhibernate to load objects, but it doesn't know anything about the objects.基本上,您的问题是您试图告诉 nhibernate 加载对象,但它对对象一无所知。 For instance, you are telling it MyMovie contains a Movie, yet it doesn't know what field Movie.title belongs to, and it doesn't know how to join in Director's with the movies because it is unmapped.例如,您告诉它 MyMovie 包含一个 Movie,但它不知道 Movie.title 属于哪个字段,并且它不知道如何将 Director's 加入到电影中,因为它没有映射。 So basically in order to pull this off without a mapping file, you need to use Criteria and result transformers to accomplish this (basically issuing a sql query and converting the results to objects via an on the fly mapping), you could encapsulate this logic in a function so it can be called in your code without being too messy, but other than that I can't see any other way around it.所以基本上为了在没有映射文件的情况下实现这一点,您需要使用标准和结果转换器来完成此操作(基本上发出 sql 查询并通过动态映射将结果转换为对象),您可以将此逻辑封装在一个 function 所以它可以在你的代码中调用而不会太乱,但除此之外我看不到任何其他方式。 Check out this post, the code is not exactly what you are trying to do (because you will have to join in directors), but it is using the same tools you will have to use... http://ayende.com/blog/2741/partial-object-queries-with-nhibernate看看这篇文章,代码并不完全是你想要做的(因为你必须加入导演),但它使用的是你必须使用的相同工具...... http://ayende.com/博客/2741/partial-object-queries-with-nhibernate

Map your class MyMovie as usual, and use disable lazyloading of Movie and Director . Map 你的 class MyMovie像往常一样,并使用禁用MovieDirector的延迟加载。 Aftter all lazy-loading for many-to-many part should work as usualy, cause for collection laziness proxy is not need.毕竟多对多部分的延迟加载应该像往常一样工作,不需要收集延迟代理的原因。

public class MyMovieMap : ClassMap<MyMovie>
{
    public MyMovieMap()
    {
        Id(x => x.id);
        References(x => x.movie);
    }
}

public class MovieMap : ClassMap<Movie>
{
    public MovieMap()
    {
        Not.LazyLoad();
        Id<int>("m_id");
        Map(x => x.title);
        HasManyToMany(x => x.Directors)
            .Table("Directs")
            .LazyLoad();
    }
}

public class DirectorMap : ClassMap<Director>
{
    public DirectorMap()
    {
        Not.LazyLoad();
        Id<int>("d_id");
        Map(x => x.name);
        HasManyToMany(x => x.DirectedMovies)
            .Table("Directs")
            .LazyLoad();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM