Ivan
Ivan

Reputation: 1284

load collection from entity object in nhibernate

I have a class called Property which holds collection of Room objects

public class Property : EntityBase<Property>
    {
        public IList<Room> _rooms;

Room mapping

public RoomMap()
        {
            Not.LazyLoad();
            Id(x => x.ID).Not.Nullable().UnsavedValue(int.MinValue).GeneratedBy.HiLo("100");
            Component(x => x.RoomDetails);
            Map(x => x.RoomType).CustomType<RoomType>();
            Component(x => x.Area);
            //HasMany(x => x.RentPriceDetails);
        }

Property map

public PropertyMap()
        {
            Not.LazyLoad();
            Id(x=>x.ID).UnsavedValue(int.MinValue).Not.Nullable().GeneratedBy.HiLo("100");
            Component(x => x.Address);
            References(x => x.OwnerOfProperty);
            HasMany(x => x._rooms).Inverse().Cascade.All();
        }

What I am trying to do is to load collection of room objects from selected property object. I use this syntax

var property = session.Query<Property>().FetchMany(x => x._rooms).First(x => x.ID == propertyId);

but only property object is loaded from database. Rooms are added in database but how to get them? If I try to access property._rooms collection is empty.

Thanks

SOLVED

Problem was with Inverse statement. I do not use bidirectional associations so I removed it and it works.

Upvotes: 0

Views: 417

Answers (2)

Ivan
Ivan

Reputation: 1284

Problem was with Inverse statement. I do not use bidirectional associations so i removed it and it works.

Upvotes: 1

Anton
Anton

Reputation: 1583

You can try to use QueryOver instead of Ling Provider:

session.QueryOver<Property>()
    .Where(property => property.Id == propertyId)
    .Fetch(property => property.Rooms).Eager
    .TransformUsing(Transformers.DistinctRootEntity)
    .SingleOrDefault<Property>();

Upvotes: 0

Related Questions