Loquacious Nhibernate and composedID foreign key - nhibernate

I have two classes that I'm trying to map in Loquacious Nhibernate.
The mapping is like the following
public class FooMap : ClassMapping<Foo>
{
Table("FooTableName");
ComposedId(compIDMapper =>
{
compIDMapper.Property(x => x.SomeInt, m => m.Column("SomeInt"));
compIDMapper.ManyToOne(x => x.SomeReference, m => m.Column("SomeReference"));
});
}
public class BarMap : ClassMapping<Bar>
{
Table("BarTableName");
Id(x => x.ID, m => m.Column("barID"));
ManyToOne(x => x.Foo, m => m.Columns( columnMapper =>
{
columnMapper.Name("SomeIntID"); //Both of these columns are in the BarTableName like they should be
columnMapper.Name("SomeReferenceID");
}));
}
But when the mappings are being built I get the following error:
Foreign key (FK554EAF2427B2CA28:BarTableName[SomeIntID])) must have same number of columns as the refe,renced primary key (FooTableName[SomeInt, SomeReference])
I'm not sure what I'm doing wrong, it looks like it should work, but I've been banging my head on this for awhile now and haven't gotten anywhere. Any ideas on what I'm doing wrong?

Finally figured this out, posting this for anyone else who comes along.
My problem was misunderstanding the columns mapper. What it is supposed to be is the following:
ManyToOne(x => x.Foo, m => m.Columns(new Action<IColumnMapper>[]
{
colMapper => colMapper.Name("SomeIntID"),
colMapper => colMapper.Name("SomeReferenceID")
}));
This solved the issue. Should have noticed it when I looked at the function signature, but I completely missed it.

And also another shorter way
ManyToOne(x => x.Foo, m => m.Columns(c=> c.Name("SomeIntID"),c => c.Name("SomeReferenceID")));

Related

NHibernate Issuing Additional UPDATE on Unidirectional One-to-Many

I'm using the new Map by Code pieces in NHibernate. My understanding was that an update was made in NHibernate 3 so that unidirectional one-to-many relationships would no longer insert null on the foreign key then update it to the correct value, as long as you set inverse=false on the collection and made the foreign key not nullable.
What I'm seeing is that NHibernate now INSERTs the correct foreign key, but it still issues an additional UPDATE that sets the foreign key to the value that was used in the insert?!?
Have I done something incorrectly in my mapping? (A user can have many passwords. The password object does not reference back to the user in my domain.)
mapper.Class<Password>(map =>
{
map.Table("Passwords");
map.Id(x => x.Id, x => { x.Generator(Generators.Native); x.Column("PasswordId"); });
map.Property(x => x.PasswordFormat, x => { x.NotNullable(true); });
map.Property(x => x.Salt, x => { x.Length(100); });
map.Property(x => x.PasswordValue, x => { x.NotNullable(true); x.Length(500); });
map.Property(x => x.CreateDate, x => { x.NotNullable(true); });
});
mapper.Class<User>(map =>
{
map.Table("Users");
map.Id(x => x.Id, x => { x.Generator(Generators.Native); x.Column("UserId"); });
map.Property(x => x.UserName, x => { x.NotNullable(true); x.Length(100); x.UniqueKey("UX_Users_Username"); });
map.Property(x => x.Email, x => { x.Length(100); x.Index("IX_Users_Email"); });
map.Property(x => x.IsAnonymous, x => { x.NotNullable(true); });
map.Property(x => x.IsApproved, x => { x.NotNullable(true); });
map.Property(x => x.LastActivityDate, x => { x.NotNullable(true); });
map.Property(x => x.CreateDate, x => { x.NotNullable(true); });
map.Set(x => x.Passwords, x => { x.Access(Accessor.Field); x.Inverse(false); x.Key(k => { k.Column("UserId"); k.NotNullable(true); k.ForeignKey("FK_Passwords_UserId"); }); x.Cascade(Cascade.All); x.Lazy(CollectionLazy.Lazy); }, x => x.OneToMany());
});
Note: This is using built-in NHibernate.Mapping.ByCode, not Fluent NHibernate.
Turns out I can accomplish this by setting k.Update(false) on the foreign key portion of the Passwords collection mapping.
The answer from hazzik on the following question queued me in.
https://stackoverflow.com/a/11576097/139694
It should be Inverse() not Inverse(false).
Inverse() means that other entity owns the relationship and it is responsible for providing data for NHibernate about the relationship while inserting/updating information i.e., if "user" is set to on inverse, "password" needs to take care of providing relationship info. to NHibernate.
For this you need to set up "User" reference property on "Password" entity. And while creating/updating the password, assign user property explicitly.
//create new password
Password objPassword = new Password();
objPassword.otherproperties =///assign
objPassword.User = <<---assign the user property
Currently, you have Inverse(false) which is default setting for NHibernate. In this case, insert statements will be executed with passwordid as null and then references are updated resulting in two operations.

Mapping IDictionary<Entity, Component> with MappingByCode

How can I map IDictionary<Entity, Component>? I've done this way:
Map<GeneralResourceType, Quantity>(x => x.BookedResources,
c =>
{
c.Key(ck => ck.Column("ProposedAction"));
c.Table("BookedResources");
},
k => k.ManyToMany(key => key.Column("ResourceTypeId")),
r => r.Component(qc => QuantityMapping.Mapping()));
(where GeneralResourceType is a mapped Entity and Quantity is a ValueObject). But during the call of BuildSession() exception is thrown:
NHibernate.MappingException : An association from the table BookedResources refers to an unmapped class: {MyNamespace}.Quantity.
Seams like it tries to find ClassMapping for Quantity, while value part mapped as Component.
First variant:
Map component in separate class inherited from ComponentMapping generic class.
Map dictionary property as follows:
Map(x => x.BookedResources, c =>
{
//any options access, cascade etc
});
Second variant (inline):
Map(x => x.BookedResources, x =>
{
//any options access, cascade etc
},
x => x.Element(),
x => x.Component(c =>
{
c.Class<Quantity>();
c.Property(p => p.Amount);
c.Property(p => p.Unit);
// any other properties
}
));

nHibernate joining multiple tables and using AliasToBean Transformer

I have a very basic need to get some data from the database and return a DTO. I found that joining multiple tables using nHibernate and "projecting" so to say, to a DTO to be quite a bit of code. After looking at several examples, most which didn't work leaving me a DTO with null values, I cam up with the following and was wondering if you nHibernate ninja's out there could tell me if there is a better way.
public IOpenIdUser GetOpenIdUser(string claimedIdentifier, IOpenIdUser openIdUserDto)
{
User user = null;
OpenIdUser openIdUser = null;
Profile profile = null;
UserType userType = null;
return
SessionWrapper.Session.QueryOver(() => user).JoinAlias(() => user.Profiles, () => profile).
JoinAlias(() => user.OpenIdUsers, () => openIdUser).JoinAlias(() => user.UserType, () => userType)
.Where(() => user.UserName == claimedIdentifier)
.SelectList(l => l
.Select(x => openIdUser.OpenIdUserId).WithAlias(() => openIdUser.OpenIdUserId)
.Select(x => user.UserId).WithAlias(() => openIdUserDto.UserId)
.Select(x => openIdUser.OpenIdClaimedIdentifier).WithAlias(
() => openIdUserDto.ClaimedIdentifier)
.Select(x => openIdUser.OpenIdFriendlyIdentifier).WithAlias(
() => openIdUserDto.FriendlyIdentifier)
.Select(x => openIdUser.OpenIdEndPoint).WithAlias(
() => openIdUserDto.OpenIdEndPoint)
.Select(x => user.UserName).WithAlias(() => openIdUserDto.UserName)
.Select(x => userType.Type).WithAlias(() => openIdUserDto.UserType)
.Select(x => profile.DisplayName).WithAlias(() => openIdUserDto.DisplayName)
.Select(x => profile.EmailAddress).WithAlias(() => openIdUserDto.EmailAddress)
.Select(x => openIdUser.DateCreated).WithAlias(() => openIdUserDto.DateCreated)
.Select(x => openIdUser.LastUpdated).WithAlias(() => openIdUserDto.LastUpdated)
.Select(x => openIdUser.UsageCount).WithAlias(() => openIdUserDto.UsageCount)
).TransformUsing(Transformers.AliasToBean<OpenIdUserDto>()).Future<OpenIdUserDto>().Single();
}
This method sits in my UserRepository and is called by my UserService. Please not that this actually works, I just think it is overkill for such a simple task. Also please note that I am new to this so if this code is crappy I apologize in advance.
If you use Queryover then this is the only way.
If you really think less lines of code are preferable, more intuitive, or sits better with you then you can do either:-
Create a DB view and create mapings files for the view with
mutable="false" in your class definition and use protected set; on your class properties
Use the LINQ provider instead e.g. .Query (see
this blog post for more info)

Retrieving distinct values in mapping

I have an existing view which has too much data in it. Unfortunately I cannot get rid of it so I need to try to work around it using my NHibernate mapping. The idea is to have NH issue the following query:
SELECT DISTINCT User_Id, Module_Id, Application_Id, RightsMask
FROM V_UserApplicationPermissions
WHERE User_Id = ?
My current mapping for this list of AccessControlEntry types looks like this:
HasMany<AccessControlEntry>(x => x.Rights)
.WithTableName("V_UserApplicationPermissions")
.KeyColumnNames.Add("User_Id")
.Component(c =>
{
c.Map(x => x.Module, "Module_Id");
c.Map(x => x.Application, "App_Id");
c.Map(x => x.Rights, "RightsMask").CustomTypeIs<ApplicationRightsType>();
})
.Not.LazyLoad();
Any thoughts on how to have NHibernate put a DISTINCT keyword in there during the query?
UPDATE: Let me share the rest of the User map that might help as to why it isn't a straight forward criteria:
WithTable("Users");
Id(x => x.Id, "UserId");
Map(x => x.Name, "UserName");
HasMany<long>(x => x.Clients)
.WithTableName("V_UserClients")
.KeyColumnNames.Add("UserId")
.AsElement("ClientId");
Olivier Coanet from the NHUsers mailing list suggested hacking it into the WithTableName which worked:
HasMany<AccessControlEntry>(x => x.Rights)
.WithTableName("(SELECT DISTINCT User_Id, Module_Id, App_Id, RightsMask FROM V_UserApplicationPermissions)")
.KeyColumnNames.Add("User_Id")
.Component(c =>
{
c.Map(x => x.Module, "Module_Id");
c.Map(x => x.Application, "App_Id");
c.Map(x => x.Rights, "RightsMask").CustomTypeIs<ApplicationRightsType>();
})

Mapping nested components in Fluent NHibernate

I have a 'User' Entity that contains an 'Address' Value Object. I have this mapping ok using FNH's Component concept. However, the Address VO also contains a Country which is another value object. I had assumed that this should be just nested as another component, but this doesn't seem to work. Can anyone tell me how I should solve this?
Code for mapping is below...
Thanks!
public UserMapping()
{
Table("Users");
Id(c => c.Id).GeneratedBy.HiLo("100");
Map(c => c.UserName).Not.Nullable().Length(64);
Map(c => c.Email).Not.Nullable().Length(128);
Map(c => c.Password).Not.Nullable().Length(256);
Map(c => c.Roles).Length(64);
Map(c => c.FirstName).Not.Nullable().Length(64);
Map(c => c.LastName).Not.Nullable().Length(64);
Map(c => c.BirthDate).Not.Nullable();
//Address
Component(x => x.Address, m =>
{
m.Map(x => x.AddressLine1).Not.Nullable();
m.Map(x => x.AddressLine2);
m.Map(x => x.City).Not.Nullable();
m.Map(x => x.Region);
m.Map(x => x.PostalCode).Not.Nullable();
//*****Country Here********
// country has Name and Code
});
}
Ah, Jimmy Bogard from the FNH mailing list showed me - it's quite straightforward. I don't know what I was doing before! Anyway, for anyone else who's interested:
Component(c => c.Address, m =>
{
m.Component(cp => cp.Country, m2 =>
{
m2.Map(x => x.Name); //etc
}
I would create a map for Country and use m.References(x => x.Country).