Nhibernate: Exception occurred getter of id

1.4k views Asked by At

Hi i got the exeption:

NHibernate.PropertyAccessException was unhandled by user code
HResult=-2146232832 Message=Exception occurred getter of SpellenDatabase.Domain.Model.Location.Id Source=NHibernate
StackTrace: bij NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object target) bij NHibernate.Tuple.Entity.AbstractEntityTuplizer.GetIdentifier(Object entity) bij NHibernate.Persister.Entity.AbstractEntityPersister.GetIdentifier(Object obj, EntityMode entityMode) bij NHibernate.Persister.Entity.AbstractEntityPersister.IsTransient(Object entity, ISessionImplementor session) bij NHibernate.Engine.ForeignKeys.IsTransient(String entityName, Object entity, Nullable1 assumed, ISessionImplementor session) bij NHibernate.Event.Default.AbstractSaveEventListener.GetEntityState(Object entity, String entityName, EntityEntry entry, ISessionImplementor source) bij NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event) bij NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event) bij NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent event) bij NHibernate.Impl.SessionImpl.SaveOrUpdate(String entityName, Object obj) bij NHibernate.Engine.CascadingAction.SaveUpdateCascadingAction.Cascade(IEventSource session, Object child, String entityName, Object anything, Boolean isCascadeDeleteEnabled) bij NHibernate.Engine.Cascade.CascadeToOne(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled) bij NHibernate.Engine.Cascade.CascadeAssociation(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled) bij NHibernate.Engine.Cascade.CascadeProperty(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled) bij NHibernate.Engine.Cascade.CascadeOn(IEntityPersister persister, Object parent, Object anything) bij NHibernate.Event.Default.AbstractSaveEventListener.CascadeAfterSave(IEventSource source, IEntityPersister persister, Object entity, Object anything) bij NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) bij NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) bij NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) bij NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event) bij NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event) bij NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event) bij NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event) bij NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent event) bij NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj) bij SpellenDatabase.DataLayer.BaseRepository1.SaveOrUpdate(T item) in C:\Users\Gebruiker\Documents\Programmeren\SpellenDatabase\SpellenDatabase.DataLayer\BaseRepository.cs:regel 27 bij SpellenDatabase.Web.Controllers.ActiviteitController.Create(Activiteit activiteit) in C:\Users\Gebruiker\Documents\Programmeren\SpellenDatabase\SpellenDatabase.Web\Controllers\ActiviteitController.cs:regel 81 bij lambda_method(Closure , ControllerBase , Object[] ) bij System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) bij System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) bij System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) bij System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.b__12() bij System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException: System.Reflection.TargetException HResult=-2146232829 Message=Het object komt niet overeen met het doeltype. Source=mscorlib StackTrace: bij System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) bij System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) bij System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) bij System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) bij NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object target) InnerException:

I got the exception when inserting a activiteit with Locationusage and referencing a existing Location. when i insert a new location i got no exception.

I hope some1 can help me with this, i can't find anything that helps

my classes:

public class Location
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IEnumerable<LocationUsage> Usage { get; set; }
}

public class LocationUsage
{
    public virtual int Id { get; set; }
    public virtual Location Location { get; set; }
    public virtual Activiteit Activiteit { get; set; }
    public virtual string Discription { get; set; }
}

public class Activiteit
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Discription { get; set; }
    public virtual ICollection<Variatie> Variaties { get; set; }
    public virtual int MinAge { get; set; }
    public virtual int MaxAge { get; set; }
    public virtual ActiviteitSoort ActiviteitSoort { get; set; }
    public virtual LocationUsage Location { get; set; }
    public virtual ICollection<MateriaalUseage> Materials { get; set; }
}

my mapping files:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SpellenDatabase.Domain.Model" assembly="SpellenDatabase.Domain">

  <class name="Location" table="Location">
    <id name="Id" type="Int32">
      <column name="Id" sql-type="int" />
      <generator class="increment" />
    </id>
    <property name="Name">
      <column name="Name" not-null="true" />
    </property>
    <set name="Usage" inverse="true">
      <key column="Id" />
      <one-to-many class="LocationUsage" />
    </set>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SpellenDatabase.Domain.Model" assembly="SpellenDatabase.Domain">

  <class name="LocationUsage" table="LocationUsage">
    <id name="Id" type="Int32">
      <column name="Id" sql-type="int" />
      <generator class="increment" />
    </id>
    <many-to-one name="Location" class="Location" column="Location" cascade="none" />
    <many-to-one name="Activiteit" class="Activiteit" column="Activiteit" unique="true" />
    <property name="Discription" />
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SpellenDatabase.Domain.Model" assembly="SpellenDatabase.Domain">

  <class name="Activiteit" table="Activiteit">

    <id name="Id">
      <column name="Id" sql-type="int" not-null="true" />
      <generator class="increment" />
    </id>

    <property name="Name">
      <column name="Name" not-null="true" />
    </property>
    <property name="Discription">
      <column name="Discription" not-null="true" />
    </property>
    <property name="MinAge" />
    <property name="MaxAge" />

    <one-to-one name="Location" class="Location" cascade="save-update" />
    <many-to-one name="ActiviteitSoort" class="ActiviteitSoort" column="ActiviteitSoort" />

    <set name="Variaties" cascade="save-update">
      <key column="Id" />
      <one-to-many class="Variatie" />
    </set>

    <set name="Materials" cascade="save-update">
      <key column="Id" />
      <one-to-many class="MateriaalUseage" />
    </set>

  </class>

</hibernate-mapping>

tables:

create table Activiteit(
Id int,
Name varchar(50),
Discription varchar(max),
MinAge int,
MaxAge int,
ActiviteitSoort int,
PRIMARY KEY (Id),
FOREIGN KEY (ActiviteitSoort) REFERENCES ActiviteitSoort(Id))

create table Location (
Id int,
Name varchar(50),
PRIMARY KEY (Id))

create table LocationUsage(
Id int,
Location int,
Activiteit int,
Discription varchar(max),
PRIMARY KEY (Id),
FOREIGN KEY (Activiteit) REFERENCES Activiteit(Id),
FOREIGN KEY (Location) REFERENCES Location(Id))

C#:

ISession session = NHibernateHelper.GetCurrentSession(); 
ITransaction tx = session.BeginTransaction();
try
{
    session.SaveOrUpdate(item);
    session.Flush();
    tx.Commit();
}
catch (Exception e)
{
    tx.Rollback();
    throw;
}
finally
{
    NHibernateHelper.CloseSession();
}
1

There are 1 answers

0
Stefan Steinegger On

You did the one-to-one wrong.

  <class name="Location" table="Location">
    <id name="Id" type="Int32">
      <column name="Id" sql-type="int" />
      <generator class="increment" />
    </id>

The location is mapped as one-to-one to the Activiteit. it means, it shares the same primary key. So they cannot both have their own generator.

Take a look at this great post by Ayende.

To be honest, I wouldn't use one-to-one. It's just too messy. Use a regular many-to-one (which adds another foreign key to the Activiteit table).