NHibernte CreateAlias为2乘法类

时间:2015-08-03 05:04:20

标签: asp.net-mvc nhibernate

我有3个班级:书,类型,作者

public class Book {
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

    public virtual string Description { get; set; }

    public virtual int MfRaiting { get; set; }

    public virtual int PageNumber { get; set; }

    public virtual IList<Genre> Genres { get; set; }


    public virtual Series Series { get; set; }

    public virtual Mind Mind { get; set; }

    public virtual IList<Author> Authors { get; set; }
    public Book() {
        Genres = new List<Genre>();
        Authors = new List<Author>();
    }

}
public class Genre {
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }
    public virtual IList<Book> Books { get; set; }

    public Genre() {
        Books=new List<Book>();
    }
}
public class Author {
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }

    public virtual string SurName { get; set; }

    public virtual string Biography { get; set; }

    public virtual IList<Book> Books { get; set; }

    public Author() {
        Books=new List<Book>();
    }
}

他的班级制作

public class BookMap : ClassMap<Book> {
    public BookMap() {
        Id(x => x.Id);
        Map(x => x.Name);
        HasManyToMany(x => x.Genres)
            .Cascade.All()
            .Table("Book_Genre");
        HasManyToMany(x => x.Authors)
            .Cascade.All()
            .Table("Book_Author");
        References(x => x.Series);
        HasOne(x => x.Mind).Constrained();
    }
}
public class GenreMap : ClassMap<Genre> {
    public GenreMap() {
        Id(x => x.Id);
        Map(x => x.Name);
        HasManyToMany(x => x.Books)
            .Cascade.All()
            .Inverse()
            .Table("Book_Genre");
    }
}
public class AuthorMap : ClassMap<Author> {
    public AuthorMap() {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.SurName);
        Map(x => x.Biography);
        HasManyToMany(x => x.Books)
            .Cascade.All()
            .Inverse()
            .Table("Book_Author");
    }
}

当我尝试写代码时 var criteria = session.CreateCriteria();     criteria.CreateAlias(“Genres”,“genre”,JoinType.LeftOuterJoin); 它效果很好,但是当我这样做时

public ActionResult Index()
{
   var criteria = session.CreateCriteria<Book>();
   criteria.CreateAlias("Genres", "genre", JoinType.LeftOuterJoin);
   criteria.CreateAlias("Authors", "author", JoinType.LeftOuterJoin);
   criteria.SetResultTransformer(new DistinctRootEntityResultTransformer());
}

我看到异常无法同时获取多个行李。我怎样才能得到该标准的结果?

感谢

1 个答案:

答案 0 :(得分:0)

我解决了这个问题!我在ISet上替换IList,并替换新的List&lt;&#34; Class&#34;&gt;在新的HashSet&lt;&#34; Class&#34;&gt;

我在这里读到了这个问题。 http://developer-should-know.tumblr.com/post/118012584847/jpa-fetching-strategies只有一个使用JOIN策略获取的集合可以是List类型,其他集合必须是Set类型。否则将抛出异常:

HibernateException:无法同时获取多个包