Hello. I'm having a problem with understanding SetFetchMode call in
Criteria API. In the 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.

The only thing that caused NHibernate to fetch entities correctly is
setting

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

but still I can write any property in SetFetchMode and will have no
exceptions.

Why Nhibernate has such an odd behaviour?

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to