Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

.net - SetFetchMode call ignored

I have a problem with SetFetchMode call in Criteria API in following query:

DetachedCriteria.For<User>()
                .Add<User>(u => u.Status == UserStatus.Live)
                .CreateAlias("UniqueId", "uid")
                .CreateAlias("Companies", "comp")
                .Add(Restrictions.Disjunction()
                                 .Add(Restrictions.Like("uid.Uid", context.Text, MatchMode.Anywhere))
                                 .Add(Restrictions.Like("comp.Name", context.Text, MatchMode.Anywhere)))
                .SetFetchMode("Companies", FetchMode.Eager));

My Classes:

public class User : EntityBase<int>
{
    public virtual UniqueId UniqueId { get; set; }

    public virtual ISet<Company> Companies { get; set; }
}

public class Company : EntityBase<int>
{
    public virtual string Name { get; set; }
}

public class UniqueId : EntityBase<int>
{
    public virtual string Uid { get; set; }
}

And mappings

public sealed class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("users");

        Id(x => x.Id).GeneratedBy.Native().Column("id");

        References(x => x.UniqueId).Column("int_unique_id_ref");

        HasMany(x => x.Companies)
            .KeyColumn("user_id")
            .Inverse()
            .AsSet();
    }
}

public sealed class CompanyMap : ClassMap<Company>
{
    public CompanyMap()
    {
        Table("company");

        Id(x => x.Id).GeneratedBy.Native().Column("id");

        Map(x => x.Name).Column("name");
    }
}

public sealed class UniqueIdMap : ClassMap<UniqueId>
{
    public UniqueIdMap()
    {
        Table("tbl_trading_partner_unique_id");

        Id(x => x.Id).GeneratedBy.Native().Column("int_id");

        Map(x => x.Uid).Column("str_unique_id");
    }
}

But after getting users list Nhibernate is quering data base again to get companies collection for each user once again. NHibernate just ignores call of SetFetchMode, because I have tried to write something like this:

.SetFetchMode("NotExistingProp", FetchMode.Eager)

Nhibernate doesn't thows any exceptions.

I also have tried to set Lazy to false in the mappings, but it also didn't help. Have no idea how to fix it, can somebody

and after that Nhibernate loaded collection with entities. But he is still ignoring SetFetchMode, I can write anything there.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The solution is not so obvious. We have changed

.CreateAlias("Companies", "comp")

to

.CreateAlias("Companies", "comp", JoinType.LeftOuterJoin)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...