HasMany cause KeyNotFoundException on Delete - nhibernate

hi i'm new to nhibernate and i read a lot of threads with similar problems, but i dont get it working.
i use oracle, nhibernate3 alpha and fluent nhibernate for mapping. i have a parent child relation. the child table has a composite id. Select, insert, update records works. Delete a parent without child records works. But deleting a parent with childs or just delete a child throws a KeyNotFoundException. it seems i miss something in my mapping?
StackTrace
bei System.Collections.Generic.Dictionary`2.get_Item(TKey key)
bei NHibernate.Engine.StatefulPersistenceContext.RemoveEntity(EntityKey key) in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\StatefulPersistenceContext.cs:Zeile 434.
bei NHibernate.Action.EntityDeleteAction.Execute() in d:\CSharp\NH\nhibernate\src\NHibernate\Action\EntityDeleteAction.cs:Zeile 86.
bei NHibernate.Engine.ActionQueue.Execute(IExecutable executable) in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:Zeile 130.
bei NHibernate.Engine.ActionQueue.ExecuteActions(IList list) in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:Zeile 113.
bei NHibernate.Engine.ActionQueue.ExecuteActions() in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:Zeile 151.
bei NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) in d:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\AbstractFlushingEventListener.cs:Zeile 241.
bei NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) in d:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\DefaultFlushEventListener.cs:Zeile 19.
bei NHibernate.Impl.SessionImpl.Flush() in d:\CSharp\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:Zeile 1524.
bei NHibernate.Transaction.AdoTransaction.Commit() in d:\CSharp\NH\nhibernate\src\NHibernate\Transaction\AdoTransaction.cs:Zeile 187.
bei LFF.Kabu.Win.Tabellenverwaltung.DataAccess.NHibernate.UnitOfWork.CommitTransaction() in C:\Demos\Tabellenverwaltung\DataAccess.NHibernate\UnitOfWork.cs:Zeile 77.
bei LFF.Kabu.Win.TabModul.DruckUndVersand.ViewModel.DruckUndVersandVM.SaveData()
below my entity classes and mappings:
public class DruckUndVersand
{
public DruckUndVersand()
{
this.RefFilters = new List<RefDruckUndVersandFilter>();
}
public virtual long Id { get; set; }
public virtual string Programm { get; set; }
public virtual string Variante { get; set; }
public virtual string Beschreibung { get; set; }
public virtual bool IsActive { get; set; }
public virtual IList<RefDruckUndVersandFilter> RefFilters { get; set; }
}
public class RefDruckUndVersandFilter
{
public virtual DruckUndVersand DruckUndVersand { get; set; }
public virtual long Rank { get; set; }
public virtual string Filter { get; set; }
#region override Equals(), GetHashCode()
//
#endregion
}
my fluent mappings look like this:
public class DruckUndVersandMapper : ClassMap<DruckUndVersand>
{
public DruckUndVersandMapper()
{
Table("Tab_DruckUndVersand");
Id(x => x.Id, "ID")
.GeneratedBy.Sequence("SEQ_DruckUndVersand");
Map(x => x.Programm).Not.Nullable().Length(255);
Map(x => x.Variante).Length(255);
Map(x => x.Beschreibung).Length(255);
Map(x => x.IsActive).Column("ISACTIVE").CustomType<YesNoType>().Length(1);
HasMany(x => x.RefFilters)
.KeyColumn("IDDruckUndVersand")
.NotFound.Ignore()
.Inverse()
.Cascade.All()
;
}
}
public class RefDruckUndVersandFilterMapper : ClassMap<RefDruckUndVersandFilter>
{
public RefDruckUndVersandFilterMapper()
{
Table("REFDruckUndVersandFILTER");
Not.LazyLoad();
Map(x => x.Filter);
CompositeId()
.KeyReference(x => x.DruckUndVersand, "IDDruckUndVersand")
.KeyProperty(x => x.Rank, "FILTERRANK");
}
}

i got it working now. the problem was my override for Equals() and GetHashCode().
public override bool Equals(object obj)
{
var toCompare = obj as RefDruckUndVersandFilter;
if (toCompare == null)
return false;
if (!GetType().Equals(toCompare.GetActualType()))
return false;
if (ReferenceEquals(this, toCompare))
return true;
return DruckUndVersand == toCompare.DruckUndVersand
&& Rank == toCompare.Rank
//&& Filter == toCompare.Filter //old causes the error
;
}
protected virtual Type GetActualType()
{
return GetType();
}
public override int GetHashCode()
{
unchecked
{
var hashcode = GetType().GetHashCode();
hashcode = (hashcode * 31) ^ (DruckUndVersand != null ? DruckUndVersand.GetHashCode() : 0);
hashcode = (hashcode * 31) ^ Rank.GetHashCode();
//hashcode = (hashcode * 31) ^ (Filter!= null ? Filter.GetHashCode() : 0); old
return hashcode;
}
}

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

How to use NHibernate DiscriminateSubClassesOnColumn and References for the same column

The Setup
I am using FluentNHibernate 1.4.0 for my NHibernate 3.3.3-SP1 mappings in a .NET4.0 library. I'm using the "table-per-inheritance" approach for my type hierarchy as follows:
-- Different process types potentially use
-- different types of reference values
CREATE TABLE ProcessTypes
(Id INT PRIMARY KEY)
-- Contains reference values for value comparisons
CREATE TABLE ProcessReferenceValues
(Id INT PRIMARY KEY IDENTITY(1,1),
ProcessTypeId INT FOREIGN KEY REFERENCES ProcessTypes(Id),
FloatReferencesValue FLOAT NULL,
IntReferenceValue INT NULL)
// POCOs
class ProcessReferenceValues
{
public virtual int Id { get; set; }
public virtual ProcessTypes ProcessType { get; set; }
public virtual float? FloatReferenceValue { get; set; }
public virtual int? IntReferenceValue { get; set; }
}
class IntProcessReferenceValues : ProcessReferenceValues { }
class FloatProcessReferenceValues : ProcessReferenceValues { }
enum ProcessTypeName : int
{
IntProcess = 1,
FloatProcess = 2
}
class ProcessTypes
{
public virtual int Id { get; set; }
public virtual ProcessTypeName Name { get; set; }
}
// FluentNHibernate Mappings
class ProcessReferenceValuesMap
: FluentNHibernate.Mapping.ClassMap<ProcessReferenceValues>
{
public ProcessReferenceValuesMap()
{
string processTypeId = "ProcessTypeId";
this.Id(x => x.Id);
this.Map(x => x.FloatReferenceValue).Nullable();
this.Map(x => x.IntReferenceValue).Nullable();
// Here is the tricky bit
this.References(x => x.ProcessType, processTypeId);
this.DiscriminateSubClassesOnColumn(processTypeId);
}
}
class IntProcessReferenceValuesMap
: FluentNHibernate.Mapping.SubclassMap<IntProcessReferenceValues>
{
public IntProcessReferenceValuesMap()
{
this.DiscriminatorValue((int)ProcessTypeName.IntProcess);
}
}
class FloatProcessReferenceValuesMap
: FluentNHibernate.Mapping.SubclassMap<FloatProcessReferenceValues>
{
public FloatProcessReferenceValuesMap()
{
this.DiscriminatorValue((int)ProcessTypeName.FloatProcess);
}
}
class ProcessPeriodTypesMap : FluentNHibernate.Mapping.ClassMap<ProcessPeriodTypes>
{
public ProcessPeriodTypesMap()
{
this.ReadOnly();
this.Id(x => x.Id, "id");
this.Map(x => x.Name, "id").ReadOnly().CustomType<PeriodTypeName>();
}
}
The Problem
While reading from the database works like a charm - the appropriate sub classes are selected correctly - saving a new process reference value gives me an exception:
// Reading
var processType =
(from type in session.Query<ProcessTypes>()
where type.Name == ProcessTypeName.IntProcess
select type).FirstOrDefault(); // OK, finds the IntProcess
var referenceValues =
(from val in session.Query<ProcessReferenceValues>()
select val).ToList(); // OK, finds the appropriate subclasses
// Inserting
var processType = new ProcessTypes
{
Id = (int)ProcessTypeName.IntProcess
};
var referenceValue = new ProcessReferenceValues
{
FloatReferenceValue = 0.7f,
IntReferenceValue = null,
ProcessType = processType // Needs the appropriate ProcessType
};
session.Save(referenceValue); // <- BOOM!
Error dehydrating property value for ProcessReferenceValues.ProcessType
Invalid Index 2 for OleDbParameterCollection with Count=2.
bei System.Data.OleDb.OleDbParameterCollection.RangeCheck(Int32 index)
bei System.Data.OleDb.OleDbParameterCollection.GetParameter(Int32 index)
bei System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index)
bei NHibernate.Type.Int32Type.Set(IDbCommand rs, Object value, Int32 index) in p:\nhibernate-core\src\NHibernate\Type\Int32Type.cs:Zeile 60.
bei NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) in p:\nhibernate-core\src\NHibernate\Type\NullableType.cs:Zeile 182.
bei NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) in p:\nhibernate-core\src\NHibernate\Type\NullableType.cs:Zeile 122.
bei NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) in p:\nhibernate-core\src\NHibernate\Type\ManyToOneType.cs:Zeile 50.
bei NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) in p:\nhibernate-core\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:Zeile 2410.
As always in hard times I summoned the gooragle and the problem seems to be that the this.DiscriminateSubClassesOnColumn(processTypeId) adds a conflict with the this.References(x => x.ProcessType, processTypeId) mapping. When I remove the former the insert is successful but I want the subclass mapping AND I also need to be able to set the ProcessReferenceValues.ProcessType when adding new instances of ProcessReferenceValues to distinguish the subclasses.
The Question
Is it possible to discriminate subclasses on a column and at the same time referencing that same column on the same type?
Help very much appreciated, there's got to be a way to do this ...
thx in advance!
classes and mappings without the problem
// POCOs
class ProcessValue
{
public virtual int Id { get; set; }
public abstract ProcessValueType Type { get; }
}
class IntProcessValue : ProcessValue
{
public virtual int? Value { get; set; }
public override ProcessValueType Type { get { return ProcessValueType.Int; } }
}
class FloatProcessValue : ProcessValue
{
public virtual float? Value { get; set; }
public override ProcessValueType Type { get { return ProcessValueType.Float; } }
}
enum ProcessValueType : int
{
Int = 1,
Float = 2
}
// FluentNHibernate Mappings
class ProcessValueMap : ClassMap<ProcessValue>
{
public ProcessValueMap()
{
string processTypeId = "ProcessValueTypeId";
Id(x => x.Id);
// just so you can query by type enum also. Querying by clr Type is already implemented
Map(x => x.Type, processTypeId).CustomType<ProcessValueType>().ReadOnly().Access.None();
DiscriminateSubClassesOnColumn(processTypeId);
}
}
class IntProcessValueMap : SubclassMap<IntProcessValue>
{
public IntProcessValueMap()
{
Map(x => x.Value).Nullable();
DiscriminatorValue((int)ProcessValueType.Int);
}
}
class FloatProcessValueMap : SubclassMap<FloatProcessValue>
{
public FloatProcessValueMap()
{
Map(x => x.Value).Nullable();
DiscriminatorValue((int)ProcessValueType.Float);
}
}

Fluent NHibernate Insert Null FK

I have a table mapping with nullable FK constraint. In my fluent mapping I am doing something like so:
public enum PlayerPosition
{
None = 0,
Forward = 1
//etc
}
Entity
public virtual PlayerPosition? Position { get; set; }
Map(x => x.Position).Column("PlayerPositionId").CustomType< PlayerPosition>();
What I would like to happen is when PlayerPosition is set to "None" Nhibernate will insert null. I am not sure how to make that happen.
i would go for IUserType:
public virtual PlayerPosition Position { get; set; }
Map(x => x.Position).Column("PlayerPositionId").CustomType<PlayerPositionUserType>();
class PlayerPositionUserType : IUserType
{
public object NullSafeGet(IDBReader reader, string[] names, object owner)
{
int? positionvalue = NHibernateUtil.Int32.NullSafeGet(reader, names[0]);
return (positionvalue.HasValue) ? (PlayerPosition)positionvalue : PlayerPosition.None;
}
public void NullSafeSet(IDBCommand cmd, object value, int index)
{
var position = (PlayerPosition)value;
if (position == PlayerPosition.None)
NHibernateUtil.Int32.NullSafeSet(cmd, null, index);
else
NHibernateUtil.Int32.NullSafeSet(cmd, (int)position, index);
}
public Type ReturnType
{
get { return typeof(PlayerPosition); }
}
public SqlType[] SqlTypes
{
get { return new [] { SqlTypeFactory.Int32 } }
}
}

NHibernate - Delete Not Peristing in the Database

i'm trying to remove an item from a one to many list and have it persist in the database. Here are the entities i have defined:
public class SpecialOffer
{
public virtual int SpecialOfferID { get; set; }
public virtual string Title { get; set; }
public virtual IList<SpecialOfferType> Types { get; private set; }
public SpecialOffer()
{
Types = new List<SpecialOfferType>();
}
}
public class SpecialOfferType
{
public virtual SpecialOffer SpecialOffer { get; set; }
public virtual Type Type { get; set; }
public virtual int MinDaysRemaining { get; set; }
#region composite id requirements
public override bool Equals(object obj)
{
if (obj == null || !(obj is SpecialOfferType))
return false;
var t = (SpecialOfferType)obj;
return SpecialOffer.SpecialOfferID == t.SpecialOffer.SpecialOfferID && Type.TypeID == t.Type.TypeID;
}
public override int GetHashCode()
{
return (SpecialOffer.SpecialOfferID + "|" + Type.TypeID).GetHashCode();
}
#endregion
}
public class Type
{
public virtual int TypeID { get; set; }
public virtual string Title { get; set; }
public virtual decimal Price { get; set; }
}
With the following fluent mappings:
public class SpecialOfferMap : ClassMap<SpecialOffer>
{
public SpecialOfferMap()
{
Table("SpecialOffers");
Id(x => x.SpecialOfferID);
Map(x => x.Title);
HasMany(x => x.Types)
.KeyColumn("SpecialOfferID")
.Inverse()
.Cascade.All();
}
}
public class SpecialOfferTypeMap : ClassMap<SpecialOfferType>
{
public SpecialOfferTypeMap()
{
Table("SpecialOfferTypes");
CompositeId()
.KeyReference(x => x.SpecialOffer, "SpecialOfferID")
.KeyReference(x => x.Type, "TypeID");
Map(x => x.MinDaysRemaining);
}
}
public class TypeMap : ClassMap<Type>
{
public TypeMap()
{
Table("Types");
Id(x => x.TypeID);
Map(x => x.Title);
Map(x => x.Price);
}
}
The problem i have is that if i remove an item from the SpecialOffer.Types collection it successfully removes it from the list but when i try to save the session the change is not persisted in the database. I'm assuming this is something to do with the composite id on the join table since i have been able to do this successfully in the past with a standard id.
I'd appreciate it if someone could show me what i'm doing wrong. Thanks
I think you have to 1) Change the cascade setting on SpecialOffer.Types to Cascade.AllDeleteOrphan() and 2) set SpecialOfferType.SpecialOffer = null when you remove it from the collection. Since the collection is the inverse side of the relationship, the many-to-one reference to SpecialOffer on SpecialOfferType has to be set to null to make it an orphan, then Cascade.AllDeleteOrphan will cause it to be deleted.

NHibernate SaveOrUpdateCopy won't insert entity with CompositeId

I have an entity with a CompositeId that won't insert new rows to the database using SaveOrUpdateCopy. The INSERT statement generated by NHibernate is populated with "?" for the value of every field. It inserts fine with SaveOrUpdate, it updates fine with either SaveOrUpdateCopy or SaveOrUpdate, and any entity without a CompositeId inserts/updates fine with SaveOrUpdateCopy. I don't want to create an if/then looking for entities with CompositeId to decide if it should use SaveOrUpdate or SaveOrUpdateCopy. Is there some trick to getting SaveOrUpdateCopy to work with entities with CompositeId?
Here's the code (names changed to protect the innocent):
public class MyEntity
{
public virtual Int32 FirstProperty { get; set; }
public virtual string SecondProperty { get; set; }
public virtual string DataText { get; set; }
public override int GetHashCode( )
{
int hashCode = 0;
hashCode = hashCode ^ FirstProperty.GetHashCode() ^
SecondProperty.GetHashCode();
return hashCode;
}
public override bool Equals( object obj )
{
MyEntity toCompare = obj as MyEntity;
if( toCompare == null )
{
return false;
}
return ( GetHashCode() != toCompare.GetHashCode() );
}
}
public MyEntityMap()
{
CompositeId()
.KeyProperty(x => x.FirstProperty, "first_property")
.KeyProperty(x => x.SecondProperty, "second_property");
Map(x => x.DataText, "data_text")
.Nullable();
Table("dbo.my_entity");
}
Database call:
public MyEntity GetMyEntity(long firstProperty, string secondProperty)
{
using (var session = sessionFactory.OpenSession())
{
var result = from entity in
session.Linq()
where entity.FirstProperty == firstProperty
&& entity.SecondProperty== secondProperty
select entity;
return result.Count() > 0 ? result.First() : null;
}
}
Database save:
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
try
{
session.SaveOrUpdateCopy(entity);
transaction.Commit();
}
catch(Exception ex)
{
transaction.Rollback();
throw;
}
}
}
Add a version property to the composite key class, see this article for an in-depth explanation.
Hi I use compositeId with FluentnHibernate but my implementation of the Equals and GetHashCode is different. This one class that have 7 fields as Key:
public class PresupuestoGastoPromocion
{
public PresupuestoGastoPromocion() { }
public virtual int Año { get; set; }
public virtual int Mes { get; set; }
public virtual int PartidaId { get; set; }
public virtual string Equipo { get; set; }
public virtual string CodFamilia { get; set; }
public virtual string GDP { get; set; }
public virtual int TipoPresupuestoId { get; set; }
public virtual float Monto { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
var t = obj as PresupuestoGastoPromocion;
if (t == null)
return false;
if (CodFamilia == t.CodFamilia && Año == t.Año && Mes == t.Mes && TipoPresupuestoId == t.TipoPresupuestoId && Equipo == t.Equipo && PartidaId == t.PartidaId && GDP == t.GDP)
return true;
return false;
}
public override int GetHashCode()
{
return (CodFamilia + "|" + Año + "|" + Mes + "|" + TipoPresupuestoId + "|" + Equipo + "|" + PartidaId + "|" + GDP).GetHashCode();
}
}
public class PresupuestoGastoPromocionMap : ClassMap<PresupuestoGastoPromocion>
{
public PresupuestoGastoPromocionMap()
{
Table("PresupuestoGastoPromocion");
CompositeId()
.KeyProperty(x => x.Año)
.KeyProperty(x => x.Mes)
.KeyProperty(x => x.TipoPresupuestoId)
.KeyProperty(x => x.CodFamilia, "Cod_Familia")
.KeyProperty(x => x.Equipo)
.KeyProperty(x => x.GDP)
.KeyProperty(x => x.PartidaId);
Map(x => x.Monto).Column("Monto");
}
}
I hope this will help you.