i have the following model:
public class FlatMap : ClassMap<Flat>
{
public FlatMap()
{
Id(m => m.FlatID).GeneratedBy.Identity();
Map(m => m.Name);
Map(m => m.Notes);
Map(m => m.Released);
}
}
public class BuildingMap : ClassMap<Building>
{
public BuildingMap()
{
Id(i => i.BuildingID).GeneratedBy.Identity();
Map(m => m.Name);
HasMany<Flat>(m => m.Flats).Cascade.All().KeyColumn("BuildingID").Not.LazyLoad();
}
}
public class ContractMap : ClassMap<Contract>
{
public ContractMap()
{
Id(m => m.ContractID).GeneratedBy.Identity();
Map(m => m.Amount);
Map(m => m.BeginIn);
Map(m => m.EndIn);
References(m => m.RentedFlat);
}
}
how can i make the following query using fluent nhibernate ?
Select * From Contract
Inner Join Flat On Contract.RentedFlatID = Flat.ID
Inner Join Building On Building.BuildingID = Flat.BuildingID
Where Building.BuildingID = #p0
especially there no reference from Flat to Building?? and i don't want it to be !
of course the reference i am talking about in order to be able to do something like this
var criteria = session.CreateCriteria<Contract>().CreateCriteria ("RentedFlat").CreateCriteria ("Building"/*there is no such property in Flat class*/);
i solved a problem, but not the way as i think is good.
but i will make this as an answer until someone provide me a better solution.
i add a property BuildingID to the Flat class, and modified the mapping class to :
Map(m => m.BuildingID);
now i can do the following query:
criteria.CreateCriteria("RentedFlat")
.Add(Restrictions.Eq("BuildingID", selectedBuilding.BuildingID));
Related
why are the children eager loaded ?
This is the relation :
public class Order
{
private IList<Product> _Product;
public virtual IReadOnlyCollection<Product> Products { get => _Product.ToList(); }
}
public class Product
{
private IList<Item> _Item;
protected internal virtual IReadOnlyCollection<Item> Items { get => _Item.ToList(); }
}
public class Item
{
private string _name;
public string Name { get => _name; }
private decimal _quantity;
public decimal Quantity { get => _quantity; }
}
Some method that gets the aggregate from DB
void Get(aggregateId)
{
var q = await Session.GetAsync<TAggregate>(aggregateId, cancellationToken);
var y = q as Order;
if (NHibernateUtil.IsInitialized(y.Products)) /// => TRUE
}
The Configurations for Order and Product
OrderConfiguration :
mapping
.HasMany<Product>(Reveal.Member<Order>("Products"))
.Access.LowerCaseField(Prefix.Underscore)
.Cascade.AllDeleteOrphan()
.Not.KeyNullable()
.Not.KeyUpdate()
.LazyLoad();
ProductConfiguration
mapping
.HasMany<Item>(Reveal.Member<Product>("Items"))
.LazyLoad()
.Component(
composit =>
{
composit
.Map(instruction => instruction.Name)
.Column("Name")
.Not.Nullable();
composit
.Map(instruction => instruction.Quantity)
.Column("Quantity")
.Not.Nullable();
});
Why does NHibernate eager loads all the data instead of waiting for it to be loaded only if needed ?
How can this be made lazy ?
Your use of ToList() on the _Product and _Item collections is inadvertently triggering the lazy loading.
How to map this Dictionary using mapping-by-code:
public class User
{
public virtual Dictionary<Option, bool> Options { get; set; } }
}
Database looks like this:
User UserOptions Option
--- --- ---
Id Id Id
UserID
OptionID
bool_column
I tried that mapping (looking here):
Map(x => x.Dictionary,
m =>
{
m.Key(k => k.Column("UserID"));
m.Table("UserOptions");
},
k => k.ManyToMany(m =>
{
m.Column("OptionID");
}),
v => v.Element(m =>
{
m.Column("bool_column");
})
);
But there is an error:
An association from the table UserOptions refers to an unmapped class: System.Boolean
OK, it is bug.
Solved by using xml-mapping for User, and adding configuration.AddXmlFile("Mappings/User.hbm.xml");
I'm getting the following error message:
NHibernate.HibernateException: NHibernate.HibernateException: Unable to resolve property: Id.
This error is thrown from the following line of code:
User userFound = session.QueryOver<User>()
.Where(x => x.Id == testObjects.TestUser.Id)
.SingleOrDefault();
My abbreviated mappings are as follows:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("USER_HEADER");
Id(x => x.Id, "USER_ID")
.GeneratedBy.Foreign("UserLocation");
HasOne(x => x.UserLocation)
.PropertyRef(x => x.Id)
.Cascade.All();
}
}
public class LocationMap : ClassMap<Location>
{
public LocationMap()
{
Table("LOC_HEADER");
Id(x => x.Id, "LOC_ID");
HasOne(x => x.User)
.PropertyRef(x => x.Id);
}
}
I was able to query a User object before I added this relationship to Location so I know it has something to do with it but I'm not sure what exactly. I can successfully create a User object that is tied to a Location but cannot query it. Using ISession.Get produces the same error as the above QueryOver statement.
Below is the overall unit test I am running that is failing:
public void Can_Create_User()
{
using (NHibernate.ISession session = SessionFactory.GetCurrentSession())
{
using (NHibernate.ITransaction tran = session.BeginTransaction())
{
session.Save(testObjects.TestValidationDetail);
session.Save(testObjects.TestUser);
tran.Commit();
}
}
using (NHibernate.ISession session = SessionFactory.GetCurrentSession())
{
User userFound = session.QueryOver<User>().Where(x => x.Id == testObjects.TestUser.Id).SingleOrDefault();
Assert.IsNotNull(userFound);
Assert.AreEqual(userFound.Id, userFound.UserLocation.Id);
}
}
It turns out this was caused by me incorrectly using PropertyRef. In my instance I did not need to use this. The error was being generated because there was no property named Id but there was an ID named Id. I corrected my issues by changing my mappings to:
HasOne(x => x.UserLocation)
.PropertyRef(x => x.Id)
.Cascade.All();
to
HasOne(x => x.UserLocation)
.Cascade.All();
and
HasOne(x => x.User)
.PropertyRef(x => x.Id);
to
HasOne(x => x.User)
PropertyRef maps to property-ref is a legacy feature, it is meant to allow you to create many-to-one associations when the association is not done on the primary key of the association.
I am guessing you want to specify on what property the join is to be made and that is why you used PropertyRef.. if you are using Nhibernates default convention in the mapping for the Id of UserLocation you dont need to explicitly specify the property.. if you are explicitly giving the column name then you need to do the same here, but in that case you need to specify the exact same column name.
Hope that helps..
I want to map a class that result in a left outer join and not in an innner join.
My composite user entity is made by one table ("aspnet_users") and an some optional properties in a second table (like FullName in "users").
public class UserMap : ClassMap<User> {
public UserMap() {
Table("aspnet_Users");
Id(x => x.Id, "UserId").GeneratedBy.Guid();
Map(x => x.UserName, "UserName");
Map(x => x.LoweredUserName, "LoweredUserName");
Join("Users",mm=>
{
mm.Map(xx => xx.FullName);
});
}
}
this mapping result in an inner join select so no result come out is second table as no data. I'd like to generate an left join.
Is this possible only at query level?
Try the Optional() method.
Join("Users", m =>
{
m.Optional();
m.Map(x => x.FullName);
});
Only this did work for me (NH 3.3):
Join("OuterJoinTable",
m =>
{
m.Fetch.Join().Optional();
m.KeyColumn("ForeignKeyColumn");
m.Map(t => t.Field, "FieldName");
});
I have the following mapping for a set of contact classes based off an abstract Contact class implementation.
public class ContactMapping : ClassMap<Contact> {
public ContactMapping() {
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.CreatedDate).Not.Nullable();
Map(x => x.Value).Not.Nullable();
Map(x => x.Level).Not.Nullable();
Map(x => x.Comments);
DiscriminateSubClassesOnColumn("ContactType");
}
}
public class PhoneContactMapping : SubclassMap<PhoneContact> {
public PhoneContactMapping() {
Map(p => p.PhoneType);
DiscriminatorValue("PhoneContact");
}
}
public class EmailContactMapping : SubclassMap<EmailContact> {
public EmailContactMapping() {
DiscriminatorValue("EmailContact");
}
}
public class WebsiteContactMapping : SubclassMap<WebsiteContact> {
public WebsiteContactMapping() {
DiscriminatorValue("WebsiteContact");
}
}
I have an entity class that HasMany EmailContact(s), WebsiteContact(s), and PhoneContact(s).
public class ContactableEntityMapping: ClassMap<ContactableEntity> {
public ContactableEntityMapping() {
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.CreatedDate).Not.Nullable();
Map(x => x.Comment);
HasMany<EmailContact>(x => x.EmailContacts).AsBag().Not.LazyLoad().Where("ContactType='EmailContact'");
HasMany<PhoneContact>(x => x.PhoneContacts).AsBag().Not.LazyLoad().Where("ContactType='PhoneContact'");
HasMany<WebsiteContact>(x => x.WebsiteContacts).Not.LazyLoad().AsBag().Where("ContactType='WebsiteContact'");
HasManyToMany(x => x.Addresses).AsSet();
}
}
If i do not specify there .Where() clauses the class ends up coming back with all rows mapped to EmailContact (since it is first in the listing) if LazyLoading is used, and if lazy-loading is not used I receive an exception as it attempts to cast the classes to the wrong type.
Obviously this is because the SQL executed is not passing in the additional where clause unless I specify it in the mapping. Am I missing something in my mapping, or is the Where mess just something we need to live with?
Thanks for the help!