NHibernate update all child rows when adding a new child - nhibernate

I'm struggling with a problem related to parent-child mapping.
I think I did everything correctly, but every time I add a child and save the parent, nhibernate execute an insert and then an update for all the rows in the child list.
here is my classes and mapping
public class Cliente : EntityBase
{
public virtual string Cognome { get; set; }
public virtual string Nome { get; set; }
public virtual int AttivitaId { get; set; }
public virtual DateTime? DittaAssunzioneData { get; set; }
public virtual decimal Stipendio { get; set; }
public virtual IList<ClienteChiusura> Chiusure { get; protected set; }
public virtual void AddTelefonata(Telefonata telefonata)
{
if (Telefonate == null)
Telefonate = new List<Telefonata>();
//NHibernate impedence
telefonata.Cliente = this;
Telefonate.Add(telefonata);
}
public virtual void RemoveTelefonata(int telefonataId)
{
if (Telefonate == null)
return;
var telefonata = Telefonate.SingleOrDefault(x => x.Id == telefonataId);
if (telefonata != null)
{
Telefonate.Remove(telefonata);
telefonata.Cliente = null;
}
}
}
public class Telefonata : EntityBase
{
public virtual Cliente Cliente { get; set; }
public virtual string Descrizione { get; set; }
public virtual DateTime? Data { get; set; }
public virtual bool Ingresso { get; set; }
public virtual string Utente { get; set; }
public virtual DateTime? UtenteData { get; set; }
public virtual TelefonataTipo TelefonataTipo { get; set; }
}
public class TelefonataTipo : EntityBase
{
public virtual string Nome { get; set; }
}
public abstract class EntityBase
{
public virtual int Id { get; private set; }
//ctor
protected EntityBase()
{ }
public override int GetHashCode()
{
return Id.GetHashCode();
}
public override bool Equals(object obj)
{
return (obj != null && obj.GetType() == GetType() && ((EntityBase)obj).Id == Id);
}
public static bool operator ==(EntityBase entity1, EntityBase entity2)
{
//cast come object altrimenti ho un loop ricorsivo
if ((object)entity1 == null && (object)entity2 == null)
return true;
if ((object)entity1 == null || (object)entity2 == null)
return false;
if (entity1.GetType() != entity2.GetType())
return false;
if (entity1.Id != entity2.Id)
return false;
return true;
}
public static bool operator !=(EntityBase entity1, EntityBase entity2)
{
return (!(entity1 == entity2));
}
}
And the mapping is as follow
public class ClienteMapping : MappingBase<Evoltel.PuntoQuinto.Domain.Entities.Cliente.Cliente>
{
public ClienteMapping()
: base("CLIENTI")
{
Id(x => x.Id, c => { c.Column("CLIENTE_ID"); c.Generator(Generators.Native, g => g.Params(new { sequence = "GEN_CLIENTI_ID" })); });
Property(x => x.Cognome, c => c.Column("COGNOME"));
Property(x => x.Nome, c => c.Column("NOME"));
Property(x => x.AttivitaId, c => c.Column("ATTIVITA_ID"));
Property(x => x.DittaAssunzioneData, c => c.Column("D_ASSUNZIONE_DATA"));
Property(x => x.Stipendio, c => c.Column("D_STIPENDIO"));
Bag(x => x.Telefonate, map =>
{
map.Inverse(true);
map.Table("TELEFONATE");
map.Cascade(Cascade.All | Cascade.DeleteOrphans);
map.Key(k => k.Column("CLIENTE_ID"));
map.Lazy(CollectionLazy.NoLazy);
}, r => r.OneToMany());
}
public class TelefonataMapping : MappingBase<Evoltel.PuntoQuinto.Domain.Entities.Cliente.Telefonata>
{
public TelefonataMapping()
: base("TELEFONATE")
{
Id(x => x.Id, c => { c.Column("TELEFONATA_ID"); c.Generator(Generators.Native, g => g.Params(new { sequence = "GEN_TELEFONATE_ID" })); });
ManyToOne(x => x.Cliente, c => c.Column("CLIENTE_ID"));
Property(x => x.Descrizione, c => c.Column("DESC_LUNGA"));
Property(x => x.Data, c => c.Column("TELEFONATA_DATA"));
Property(x => x.Ingresso, c => { c.Column("INGRESSO"); c.Type<TrueFalseType>(); });
Property(x => x.Utente, c => c.Column("UTENTE_NOME"));
Property(x => x.UtenteData, c => c.Column("UTENTE_DATA"));
ManyToOne(x => x.TelefonataTipo, c => { c.Column("TELEFONATA_TIPO_ID"); c.NotFound(NotFoundMode.Ignore); });
}
}
public abstract class MappingBase<T> : ClassMapping<T> where T : EntityBase
{
protected MappingBase(string tableName)
{
Table(tableName);
Lazy(false);
DynamicUpdate(true);
}
}
If I do not use Cascade.All the new Child is not saved (and for what I understood of NHibernate it's correct)
What is that I'm not seeing?

Related

Fluent Nhibernate Composite Key mapping error

Composite Key mapping is not working in below scenario.
Database tables are as below.
Employee { Emp_ID, Name, Role_ID } (Role_ID is foreign key from Role table);
Leave { Leave_ID, Leave_Date, Leave_Comment};
Employee_Leave { Emp_ID, Leave_ID, Approval }; (EMP_ID and Leave_ID are composite key from Employee and Leave table respectively)
Entity classes are as below.
class Employee
{
public virtual string ID { get; set; }
public virtual string Name { get; set; }
public virtual Role EmpRole { get; set; }
}
public class Leave
{
virtual public Int16 LeaveID { get; set; }
virtual public String LeaveDate { get; set; }
virtual public String Comment { get; set; }
}
public class EmployeeLeaveApproval
{
public virtual string EMP_ID { get; set; }
public virtual int Leave_ID { get; set; }
public virtual string Approval { get; set; }
}
Mapping classes are as below.
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Table("Employee");
Id(x => x.ID, "ID");
Map(x => x.Name, "NAME");
References(x => x.EMPRole, "ROLE_ID").Not.LazyLoad();
}
}
public class LeaveMap : ClassMap<Leave>
{
public LeaveMap()
{
Table("Leave");
Id(x => x.LeaveID, "LEAVE_ID");
Map(x => x.LeaveDate, "LEAVE_DATE");
Map(x => x.Comment, "LEAVE_COMMENT");
}
}
Below class mapping is working fine.
public class EmployeeLeaveApprovalMap : ClassMap<EmployeeLeaveApproval>
{
public EmployeeLeaveApprovalMap()
{
Table("Employee_Leave");
Id(x => x.EMP_ID, "EMP_ID");
Map(x => x.Leave_ID, "LEAVE_ID");
Map(x => x.Approval, "Approval");
}
}
Below class mapping is not working.
public class EmployeeLeaveApprovalMap : ClassMap<EmployeeLeaveApproval>
{
public EmployeeLeaveApprovalMap()
{
Table("Employee_Leave");
CompositeId()
.KeyProperty(x => x.EMP_ID, "EMP_ID")
.KeyProperty(x => x.Leave_ID, "LEAVE_ID");
Map(x => x.Approval, "Approval");
}
}
Getting error "Database was not configured through Database method." while calling method BuildSessionFactory.
Many many thanks in advance for any help.
Found solution rather to say found the mistake i am doing.
Equal and GetHashCode methods are left being implemented in my code.
Below is the corrected entiity,
public class EmployeeLeaveApproval : Object
{
public virtual string EMP_ID { get; set; }
public virtual int Leave_ID { get; set; }
public virtual string Approval { get; set; }
public EmployeeLeaveApproval() {}
public override bool Equals(object obj)
{
if (obj == null)
return false;
EmployeeLeaveApproval EL = (EmployeeLeaveApproval)obj;
if (EL == null)
return false;
if (EMP_ID == EL.EMP_ID && Leave_ID == EL.Leave_ID)
return true;
return false;
}
public override int GetHashCode()
{
return (EMP_ID + "|" + Leave_ID).GetHashCode();
}
}
regards..Dharmendra

NHibernate Table Per Subclass results in nonsensical INSERT statement - What am I doing wrong?

I have the following entities:
public abstract class User : IIdentity
{
private readonly UserType _userType;
public virtual int EntitySK { get; set; }
public virtual int TenantSK { get; set; }
public abstract string Name { get; set; }
public virtual PublicKey PublicKey { get; set; }
public abstract string AuthenticationType { get; set; }
public virtual bool IsAuthenticated { get; protected internal set; }
public virtual bool LoginEnabled { get; set; }
public virtual bool LockedOut { get; set; }
public virtual int NumberOfFailedLoginAttempts { get; set; }
// Hibernate requires this constructor
protected User()
{
this._userType = this is PersonUser ? Models.UserType.Person : Models.UserType.Client;
this.LoginEnabled = true;
}
protected User(UserType userType)
{
this._userType = userType;
this.LoginEnabled = true;
}
public virtual UserType UserType
{
get { return this._userType; }
set
{
if(value != this._userType)
throw new InvalidOperationException("Attempted to load " + value + " into " + this._userType + "User.");
}
}
}
public class PersonUser : User
{
public virtual string Domain { get; set; }
public override string Name { get; set; }
public virtual byte[] Password { get; set; }
public virtual byte[] Pepper { get; set; }
public virtual string EmailAddress { get; set; }
public virtual int PersonSK { get; set; }
public override string AuthenticationType { get; set; }
public PersonUser() : base(UserType.Person) { }
}
public class ClientUser : User
{
public override string Name { get; set; }
public virtual string SharedSecret { get; set; }
public virtual ISet<string> Scopes { get; set; }
public virtual ISet<GrantType> AuthorizedGrantTypes { get; set; }
public virtual ISet<Uri> RegisteredRedirectUris { get; set; }
public virtual int AuthorizationCodeValiditySeconds { get; set; }
public virtual int AccessTokenValiditySeconds { get; set; }
public ClientUser() : base(UserType.Client) { }
}
I map these entities using the following Hibernate Conformist mapping:
public class UserMapping : ClassMapping<User>
{
public UserMapping()
{
LogManager.GetLogger().Info("Initialized User mapping.");
this.Table("Authentication_Users");
this.Id(u => u.EntitySK,
m => {
m.Column("UserSK");
m.Generator(Generators.Identity);
m.UnsavedValue(0);
});
this.Property(u => u.TenantSK,
m => {
m.Column("TenantSK");
m.NotNullable(true);
});
this.Property(u => u.PublicKey,
m => {
m.Column("PublicKey");
m.Type<PublicKeyCustomType>();
m.NotNullable(false);
m.Lazy(true);
});
this.Property(u => u.UserType,
m => {
m.Column("UserType");
m.NotNullable(true);
m.Type<EnumCustomType<UserType>>();
});
this.Property(u => u.LoginEnabled,
m => {
m.Column("LoginEnabled");
m.NotNullable(true);
});
this.Property(u => u.LockedOut,
m => {
m.Column("LockedOut");
m.NotNullable(true);
});
this.Property(u => u.NumberOfFailedLoginAttempts,
m => {
m.Column("NumberOfFailedLoginAttempts");
m.NotNullable(true);
});
this.Discriminator(d => d.Column("UserType"));
}
}
public class PersonUserMapping : SubclassMapping<PersonUser>
{
public PersonUserMapping()
{
LogManager.GetLogger().Info("Initialized PersonUser mapping.");
this.DiscriminatorValue((int)UserType.Person);
this.Join(
"PersonUser",
j =>
{
j.Table("Authentication_Users_PersonUsers");
j.Key(m => {
m.Column("UserSK");
m.NotNullable(true);
m.OnDelete(OnDeleteAction.Cascade);
m.Unique(true);
m.Update(false);
});
j.Property(u => u.Domain,
m => {
m.Column("DomainName");
m.NotNullable(false);
});
j.Property(u => u.Name,
m => {
m.Column("Username");
m.NotNullable(true);
});
j.Property(u => u.Password,
m => {
m.Column("Password");
m.NotNullable(false);
m.Lazy(true);
});
j.Property(u => u.Pepper,
m => {
m.Column("Pepper");
m.NotNullable(false);
m.Lazy(true);
});
j.Property(u => u.EmailAddress,
m => {
m.Column("EmailAddress");
m.NotNullable(false);
});
j.Property(u => u.PersonSK,
m => {
m.Column("PersonSK");
m.NotNullable(false);
});
j.Property(u => u.AuthenticationType,
m => {
m.Column("AuthenticationType");
m.NotNullable(true);
});
}
);
}
}
public class ClientUserMapping : SubclassMapping<ClientUser>
{
public ClientUserMapping()
{
LogManager.GetLogger().Info("Initialized ClientUser mapping.");
this.DiscriminatorValue((int)UserType.Client);
this.Join(
"ClientUser",
j =>
{
j.Table("Authentication_Users_ClientUsers");
j.Key(m => {
m.Column("UserSK");
m.NotNullable(true);
m.OnDelete(OnDeleteAction.Cascade);
m.Unique(true);
m.Update(false);
});
j.Property(u => u.Name,
m => {
m.Column("DisplayName");
m.NotNullable(true);
});
j.Property(u => u.SharedSecret,
m => {
m.Column("SharedSecret");
m.NotNullable(true);
});
j.Property(u => u.AuthorizationCodeValiditySeconds,
m => {
m.Column("AuthorizationCodeValiditySeconds");
m.NotNullable(true);
});
j.Property(u => u.AccessTokenValiditySeconds,
m => {
m.Column("AccessTokenValiditySeconds");
m.NotNullable(true);
});
j.Set(u => u.Scopes,
s => {
s.Fetch(CollectionFetchMode.Join);
s.Lazy(CollectionLazy.Lazy);
s.Table("Authentication_Users_ClientUsers_Scopes");
s.Key(m => {
m.Column("UserSK");
m.NotNullable(true);
});
},
r => r.Element(m => {
m.Column("Scope");
m.NotNullable(true);
m.Unique(true);
}));
j.Set(u => u.AuthorizedGrantTypes,
s => {
s.Fetch(CollectionFetchMode.Join);
s.Lazy(CollectionLazy.Lazy);
s.Table("Authentication_Users_ClientUsers_AuthorizedGrantTypes");
s.Key(m => {
m.Column("UserSK");
m.NotNullable(true);
});
},
r => r.Element(m => {
m.Column("GrantType");
m.NotNullable(true);
m.Unique(true);
m.Type<EnumCustomType<GrantType>>();
}));
j.Set(u => u.RegisteredRedirectUris,
s => {
s.Fetch(CollectionFetchMode.Join);
s.Lazy(CollectionLazy.Lazy);
s.Table("Authentication_Users_ClientUsers_RegisteredRedirectUris");
s.Key(m => {
m.Column("UserSK");
m.NotNullable(true);
});
},
r => r.Element(m => {
m.Column("Uri");
m.NotNullable(true);
m.Unique(true);
m.Type<UriCustomType>();
}));
}
);
}
}
The EnumCustomType is an IUserType that maps C# enums to integer columns.
I formulated this design and mapping after much research and consultation with the NHibernate reference documentation and this blog (specific page) (summary). I am sure this is the entity design I want, but of course it's possible (likely?) I got the mapping wrong.
When I start up and configure NHibernate, it loads the mappings and does not complain. The log output has no warnings about the mappings. However, when I create a PersonUser, assign values to all of its properties, and Add it to the ISession, the most peculiar thing happens:
2014-01-16 00:58:34,465 DEBUG NHibernate.AdoNet.AbstractBatcher.Generate() - Building an IDbCommand object for the SqlString: INSERT INTO Authentication_Users (TenantSK, DisplayName, PublicKey, LoginEnabled, LockedOut, NumberOfFailedLoginAttempts, Username, AuthenticationType, UserType) VALUES (?, ?, ?, ?, ?, ?, ?, ?, '1'); select SCOPE_IDENTITY()
2014-01-16 00:58:34,472 DEBUG NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate() - Dehydrating entity: [Models.PersonUser#<null>]
2014-01-16 00:58:34,475 DEBUG NHibernate.Type.NullableType.NullSafeSet() - binding '0' to parameter: 0
2014-01-16 00:58:34,478 DEBUG NHibernate.Type.NullableType.NullSafeSet() - binding 'nick.williams' to parameter: 1
2014-01-16 00:58:34,482 DEBUG NHibernate.Type.NullableType.NullSafeSet() - binding 'PublicKey' to parameter: 3
2014-01-16 00:58:34,485 DEBUG NHibernate.Type.NullableType.NullSafeSet() - binding 'True' to parameter: 4
2014-01-16 00:58:34,486 DEBUG NHibernate.Type.NullableType.NullSafeSet() - binding 'False' to parameter: 5
2014-01-16 00:58:34,487 DEBUG NHibernate.Type.NullableType.NullSafeSet() - binding '0' to parameter: 6
2014-01-16 00:58:34,488 DEBUG NHibernate.Type.NullableType.NullSafeSet() - binding 'nick.williams' to parameter: 8
NHibernate.PropertyValueException : Error dehydrating property value for Models.PersonUser.Name
----> System.IndexOutOfRangeException : Invalid index 8 for this SqlParameterCollection with Count=8.
at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index)
at NHibernate.Persister.Entity.AbstractEntityPersister.GeneratedIdentifierBinder.BindValues(IDbCommand ps)
at NHibernate.Id.Insert.AbstractReturningDelegate.PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Action.EntityIdentityInsertAction.Execute()
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)
Importantly, take a look at that SQL generated:
INSERT INTO Authentication_Users (TenantSK, DisplayName, PublicKey, LoginEnabled, LockedOut, NumberOfFailedLoginAttempts, Username, AuthenticationType, UserType) VALUES (?, ?, ?, ?, ?, ?, ?, ?, '1')
That makes no sense. It in no way corresponds to the mapping at all. It's got columns from three different tables in an insert statement for one table. It has columns from PersonUser AND from ClientUser (which shouldn't be possible), it's binding parameters with nonexistent parameter indexes, and it's not even including all the properties I set!
I've been playing around with this for several hours now without any progress. I'm at a complete loss here. It just doesn't make any sense. Anyone seen this before? Any idea what's going on?
EDIT I forgot to mention: I'm using the discriminator here because I want to be able to get a generic User by its ID and it return the proper PersonUser or ClientUser depending on which type it is.
There are some issues with the mapping
a) You are mapping the UserType as property but you are using it as discriminator, too. But the user type value is generated by nhibernate, because of the discriminator usage... Therefore you can define the property to be generated.
this.Property(u => u.UserType,
m =>
{
m.Column("UserType");
m.NotNullable(true);
m.Generated(PropertyGeneration.Always); // this should fix it for user type
m.Type<EnumCustomType<UserType>>();
});
You should also not throw an exception on the setter... to prevent UserType being set from somewhere, simply mark the auto property setter protected internal
public virtual UserType UserType
{
get;
protected internal set;
}
b) You are mapping the Name attribute of your base class in your sub classes only and you are trying to map that property to different columns within your sub classes' tables.
Don't know if this is even possible, usually you have to map all properties of your base class to the base class table, or move the property into the subclass...
To fix this, simply remove the mappings to Name from your subclass mappings and move it to your base class mapping.
With MichaC's help, this was the code that finally worked.
Entities:
public abstract class User : IIdentity
{
// necessary because the property is read-only
private readonly UserType _userType;
// necessary because the property needs to default to true
private bool _loginEnabled = true;
//-------- These properties are mapped to database columns --------//
public virtual int ObjectId { get; set; }
public virtual int? TenantId { get; set; }
public virtual PublicKey PublicKey { get; set; }
public virtual bool LoginEnabled { get { return this._loginEnabled; } set { this._loginEnabled = value; } }
public virtual bool LockedOut { get; set; }
public virtual int NumberOfFailedLoginAttempts { get; set; }
//-------- These properties are NOT mapped to database columns --------//
public abstract string Name { get; set; }
public abstract string AuthenticationType { get; set; }
public virtual bool IsAuthenticated { get; protected internal set; }
public virtual UserType UserType
{
get { return this._userType; }
set { throw new InvalidOperationException("Property UserType is read-only."); }
}
...
}
public class PersonUser : User
{
//-------- These properties are mapped to database columns --------//
public virtual string Domain { get; set; }
protected internal virtual string Username { get { return this.Name; } set { this.Name = value; } }
public virtual byte[] Password { get; set; }
public virtual byte[] Pepper { get; set; }
public virtual string EmailAddress { get; set; }
public virtual int PersonSK { get; set; }
protected internal virtual string AuthenticationStrategy
{
get { return this.AuthenticationType; }
set { this.AuthenticationType = value; }
}
//-------- These properties are NOT mapped to database columns --------//
public override string Name { get; set; }
public override string AuthenticationType { get; set; }
}
public class ClientUser : User
{
//-------- These properties are mapped to database columns --------//
protected internal virtual string DisplayName { get { return this.Name; } set { this.Name = value; } }
public virtual string SharedSecret { get; set; }
public virtual ISet<string> Scopes { get; set; }
public virtual ISet<GrantType> AuthorizedGrantTypes { get; set; }
public virtual ISet<Uri> RegisteredRedirectUris { get; set; }
public virtual int AuthorizationCodeValiditySeconds { get; set; }
public virtual int AccessTokenValiditySeconds { get; set; }
//-------- These properties are NOT mapped to database columns --------//
public override string Name { get; set; }
public override string AuthenticationType
{
get { return AuthorizationHeaderProtocol.SignatureClientCredentials; }
set { throw new InvalidOperationException("Cannot change the authentication type for a ClientUser."); }
}
}
Mappings:
public class UserMapping : ClassMapping<User>
{
public UserMapping()
{
LogManager.GetLogger().Info("Initialized User mapping.");
this.Table("Authentication_Users");
this.Id(u => u.ObjectId,
m => {
m.Column("UserId");
m.Generator(Generators.Identity);
m.UnsavedValue(0);
});
this.Property(u => u.TenantId,
m => {
m.Column("TenantId");
m.NotNullable(false);
});
this.Property(u => u.PublicKey,
m => {
m.Column("PublicKey");
m.Type<PublicKeyCustomType>();
m.NotNullable(false);
m.Lazy(true);
});
this.Property(u => u.LoginEnabled,
m => {
m.Column("LoginEnabled");
m.NotNullable(true);
});
this.Property(u => u.LockedOut,
m => {
m.Column("LockedOut");
m.NotNullable(true);
});
this.Property(u => u.NumberOfFailedLoginAttempts,
m => {
m.Column("NumberOfFailedLoginAttempts");
m.NotNullable(true);
});
this.Discriminator(d => d.Column("UserType"));
}
}
public class PersonUserMapping : SubclassMapping<PersonUser>
{
public PersonUserMapping()
{
LogManager.GetLogger().Info("Initialized PersonUser mapping.");
this.DiscriminatorValue((int)UserType.Person);
this.Join(
"PersonUser",
j =>
{
j.Table("Authentication_Users_PersonUsers");
j.Key(m => {
m.Column("UserId");
m.NotNullable(true);
m.OnDelete(OnDeleteAction.Cascade);
m.Unique(true);
m.Update(false);
});
j.Property(u => u.Domain,
m => {
m.Column("DomainName");
m.NotNullable(false);
});
j.Property("Username", // protected internal, see NH-3485
m => {
m.Column("Username");
m.NotNullable(true);
});
j.Property(u => u.Password,
m => {
m.Column("Password");
m.NotNullable(false);
m.Lazy(true);
});
j.Property(u => u.Pepper,
m => {
m.Column("Pepper");
m.NotNullable(false);
m.Lazy(true);
});
j.Property(u => u.EmailAddress,
m => {
m.Column("EmailAddress");
m.NotNullable(false);
});
j.Property(u => u.PersonSK,
m => {
m.Column("PersonSK");
m.NotNullable(false);
});
j.Property("AuthenticationStrategy", // protected internal, see NH-3485
m => {
m.Column("AuthenticationType");
m.NotNullable(true);
});
}
);
}
}
public class ClientUserMapping : SubclassMapping<ClientUser>
{
public ClientUserMapping()
{
LogManager.GetLogger().Info("Initialized ClientUser mapping.");
this.DiscriminatorValue((int)UserType.Client);
this.Join(
"ClientUser",
j =>
{
j.Table("Authentication_Users_ClientUsers");
j.Key(m => {
m.Column("UserId");
m.NotNullable(true);
m.OnDelete(OnDeleteAction.Cascade);
m.Unique(true);
m.Update(false);
});
j.Property("DisplayName", // protected internal, see NH-3485
m => {
m.Column("DisplayName");
m.NotNullable(true);
});
j.Property(u => u.SharedSecret,
m => {
m.Column("SharedSecret");
m.NotNullable(true);
});
j.Property(u => u.AuthorizationCodeValiditySeconds,
m => {
m.Column("AuthorizationCodeValiditySeconds");
m.NotNullable(true);
});
j.Property(u => u.AccessTokenValiditySeconds,
m => {
m.Column("AccessTokenValiditySeconds");
m.NotNullable(true);
});
j.Set(u => u.Scopes,
s => {
s.Fetch(CollectionFetchMode.Join);
s.Lazy(CollectionLazy.Lazy);
s.Table("Authentication_Users_ClientUsers_Scopes");
s.Key(m => {
m.Column("UserId");
m.NotNullable(true);
});
},
r => r.Element(m => {
m.Column("Scope");
m.NotNullable(true);
m.Unique(true);
}));
j.Set(u => u.AuthorizedGrantTypes,
s => {
s.Fetch(CollectionFetchMode.Join);
s.Lazy(CollectionLazy.Lazy);
s.Table("Authentication_Users_ClientUsers_AuthorizedGrantTypes");
s.Key(m => {
m.Column("UserId");
m.NotNullable(true);
});
},
r => r.Element(m => {
m.Column("GrantType");
m.NotNullable(true);
m.Unique(true);
m.Type<EnumCustomType<GrantType>>();
}));
j.Set(u => u.RegisteredRedirectUris,
s => {
s.Fetch(CollectionFetchMode.Join);
s.Lazy(CollectionLazy.Lazy);
s.Table("Authentication_Users_ClientUsers_RegisteredRedirectUris");
s.Key(m => {
m.Column("UserId");
m.NotNullable(true);
});
},
r => r.Element(m => {
m.Column("Uri");
m.NotNullable(true);
m.Unique(true);
m.Type<UriCustomType>();
}));
}
);
}
}

nHibernate map a filtered bag to a single property

I need to map an etity to a mater tabele of a DB.
This Entity has OneToMany with another.
I need to map a Collection othe the master entity to all rows of the Child table.
But I also need to map a Property with a single row getted from the child table and filtered by a criteria that return always only one row.
Someting like a Component but in a filtered child table.
This is My Mapping:
public class Test
{
public virtual string Id { get; set; }
public virtual string Description { get; set; }
public virtual IList<TestItem> Items { get; set; }
public virtual TestItem Item { get; set; }
public Test()
{
Items = new List<TestItem>();
}
}
public class TestItem
{
public virtual string Id { get; set; }
public virtual Test Test { get; set; }
public virtual string ItemCode { get; set; }
public virtual string ItemData { get; set; }
}
public class TestMap : ClassMapping<Test>
{
public TestMap()
{
Id(x => x.Id, m => m.Column("IDTest"));
//IPOTETICAL
SomeComponent(x => x.Item, c => // How to map a filtered collection to a single property??
{
c.Key(k =>
{
k.NotNullable(true);
k.Column("IDTest");
});
**// This Is the filter**
c.Filter("itemsFilter", f => f.Condition("ItemCode = :itemsCodeValue"));
}, r => r.OneToMany(m =>
{
m.NotFound(NotFoundMode.Exception);
m.Class(typeof(TestItem));
}));
Bag(x => x.Items, c => // All Child Rows
{
c.Key(k =>
{
k.NotNullable(true);
k.Column("IDTest");
});
c.Cascade(Cascade.All | Cascade.DeleteOrphans);
c.Lazy(CollectionLazy.NoLazy);
c.Inverse(true);
}, r => r.OneToMany(m =>
{
m.NotFound(NotFoundMode.Exception);
m.Class(typeof(TestItem));
}));
}
}
public class TestItemMap : ClassMapping<TestItem>
{
public TestItemMap()
{
Id(x => x.Id, m => m.Column("IDTestItem"));
ManyToOne(x => x.Test, m =>
{
m.Column("IDTest");
m.NotNullable(false);
m.Lazy(LazyRelation.NoLazy);
});
Property(x => x.ItemCode);
Property(x => x.ItemData);
}
}
I found something about DynamicComponent but I do not know if it is for me...
http://notherdev.blogspot.it/2012/01/mapping-by-code-dynamic-component.html
Thank You!!

Fluent NHibernate automapping table-per-abstract-hierarchy / table-per-concrete-subclass

I have classes
public abstract class Content : IContent
{
public virtual Guid Id { get; protected set; }
public virtual IPage Parent { get; set; }
public virtual DateTime Created { get; set; }
/* ... */
}
public abstract class Page : Content, IPage
{
public virtual string Slug { get; set; }
public virtual string Path { get; set; }
public virtual string Title { get; set; }
/* ... */
}
public class Foo : Page, ITaggable
{
// this is unique property
// map to joined table
public virtual string Bar { get; set; }
// this is a unique collection
public virtual ISet<Page> Related { get; set; }
// this is "shared" property (from ITaggable)
// map to shared table
public virtual ISet<Tag> Tags { get; set; }
}
And as a result I'd like to have the following tables. I've tried implementing tons of different IConventions, but even the hierarchy mappings (table-per-abstract-hierarchy / table-per-concrete-subclass) seem to fail.
Content
Id
Type (discriminator)
ParentId
Created
Slug
Path
Title
Content_Tags (Tags from ITaggable)
ContentId
TagId
Content$Foo
Bar
Content$Foo_Related
ParentFooId
ChildPageId
I already have ugly, working fluent mappings, but I would like to get rid of some ugliness
public class ContentMapping : ClassMap<Content>
{
public ContentMapping()
{
Table("Content");
Id(x => x.Id).GeneratedBy.GuidComb();
References<Page>(x => x.Parent, "ParentId");
Map(x => x.Created);
DiscriminateSubClassesOnColumn("Type");
}
}
public class PageMapping : SubclassMap<Page>
{
public PageMapping()
{
Map(x => x.Slug);
Map(x => x.Path);
Map(x => x.Title);
}
}
public class ConcreteContentMapping<T> : SubclassMap<T> where T : Content, new()
{
public ConcreteContentMapping() : this(true) { }
protected ConcreteContentMapping(bool mapJoinTable)
{
DiscriminatorValue(typeof(T).FullName);
MapCommonProperties();
if(mapJoinTable)
{
MapJoinTableWithProperties(CreateDefaultJoinTableName(), GetPropertiesNotFrom(GetContentTypesAndInterfaces().ToArray()));
}
}
private void MapCommonProperties()
{
if (typeof(ITagContext).IsAssignableFrom(typeof(T)))
{
Map(x => ((ITagContext)x).TagDirectory);
}
if (typeof(ITaggable).IsAssignableFrom(typeof(T)))
{
HasManyToMany(x => ((ITaggable)x).Tags).Table("Content_Tags").ParentKeyColumn("ContentId").ChildKeyColumn("TagId").Cascade.SaveUpdate();
}
}
/* ... */
// something I would like to get rid of with automappings...
protected void MapCollectionProperty(JoinPart<T> table, PropertyInfo p)
{
var tableName = ((IJoinMappingProvider)table).GetJoinMapping().TableName + "_" + p.Name;
var elementType = p.PropertyType.GetGenericArguments()[0];
var method = table.GetType().GetMethods().Where(m => m.Name == "HasManyToMany")
.Select(m => new { M = m, P = m.GetParameters() })
.Where(x => x.P[0].ParameterType.GetGenericArguments()[0].GetGenericArguments()[1] == typeof(object))
.FirstOrDefault().M.MakeGenericMethod(elementType);
dynamic m2m = method.Invoke(table, new object[] { MakePropertyAccessExpression(p)});
m2m.Table(tableName).ParentKeyColumn("Parent" + typeof(T).Name + "Id").ChildKeyColumn("Child" + elementType.Name + "Id");
}
protected Expression<Func<T, object>> MakePropertyAccessExpression(PropertyInfo property)
{
var param = Expression.Parameter(property.DeclaringType, "x");
var ma = Expression.MakeMemberAccess(param, property);
return Expression.Lambda<Func<T, object>>(ma, param);
}
}
How do I get the same result with automappings?

NHibernate Error Message: Invalid index 3 for this SqlParameterCollection with Count=3

I have a test database design like this:
The following is the pseudo-code:
//BhillHeader
public class BillHeader
{
public BillHeader()
{
BillDetails = new List<BillDetail>();
}
public virtual int BillNo { get; set; }
public virtual IList<BillDetail> BillDetails { get; set; }
public virtual decimal Amount { get; set; }
public virtual void AddDetail(BillDetail billdet)
{
BillDetails.Add(billdet);
}
}
//BillHeader Map
public class BillHeaderMap : ClassMap<BillHeader>
{
public BillHeaderMap()
{
Table("BillHeader");
LazyLoad();
Id(x => x.BillNo).GeneratedBy.Identity().Column("BillNo");
Map(x => x.Amount).Column("Amount").Not.Nullable();
HasMany(x => x.BillDetails).KeyColumn("BillNo").Cascade.All().Inverse();
}
}
//BillDetail
public class BillDetail
{
public BillDetail() { }
public virtual int BillID { get; set; }
public virtual int SeqNo { get; set; }
public virtual BillHeader BillHeader { get; set; }
public virtual decimal Amt { get; set; }
public override bool Equals(object obj)
{
var other = obj as BillDetail;
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return this.BillID == other.BillID &&
this.SeqNo == other.SeqNo;
}
public override int GetHashCode()
{
unchecked {
int hash = GetType().GetHashCode();
hash = (hash * 31) ^ SeqNo.GetHashCode();
hash = (hash * 31) ^ BillID.GetHashCode();
return hash;
}
}
}
//BillDetail Map
public class BillDetailMap : ClassMap<BillDetail>
{
public BillDetailMap()
{
Table("BillDetail");
LazyLoad();
CompositeId().KeyProperty(x => x.BillID, "BillNo").KeyProperty(x => x.SeqNo, "SeqNo");
References(x => x.BillHeader).Column("BillNo");
Map(x => x.Amt).Column("Amt").Not.Nullable();
}
}
//-----------------------------------------------------------------------------------------------------------------------------
//Program
public createBillNo()
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession()) {
using (var sqlTrans = session.BeginTransaction()) {
BillHeader billNo1 = new BillHeader() { Amount = 2500.00M};
BillDetail bh11 = new BillDetail() { SeqNo = 1, Amt = 200.00M };
BillDetail bh12 = new BillDetail() { SeqNo = 2, Amt = 300.00M };
BillDetail bh13 = new BillDetail() { SeqNo = 3, Amt = 500.00M };
AddBillDetailsToBillHeader(billNo1, bh11, bh12, bh13);
session.SaveOrUpdate(billNo1);
sqlTrans.Commit();
}
}
}
private void AddBillDetailsToBillHeader(BillHeader billHeader, params BillDetail[] billDetails)
{
foreach (var billdet in billDetails) {
billHeader.AddDetail(billdet);
billdet.BillHeader = billHeader;
}
}
When I run this I'm getting the following exception:
Invalid index 3 for this SqlParameterCollection with Count=3
Please help me to resolve this issue.
most probably because column "BillNo" is mapped twice, it tries to add 2 parameter for 1 column, hence the outOfRange error. move the reference into the compositekey
CompositeId()
.KeyReference(x => x.BillHeader, "BillNo")
.KeyProperty(x => x.SeqNo, "SeqNo");
// References(x => x.).Column("BillNo"); <-- Remove