After adding a few lines to one of my unit tests, which should
normally have worked it appears to be killing nHibernate and I'm not
sure whats wrong.
I have this one unit test which just creates a new
"AuthorizationLevel" object and saves it to the database. If I do not
add any parent authorizations then I do not get this error (meaning
the parent authorizations IList collection is NULL).
Here is the error I am getting:
NHibernate: INSERT INTO [dbo].[AuthorizationLevels]
([AuthorizationShortDesc], [AuthorizationDescription]) VALUES (@p0,
@p1); select SCOPE_IDENTITY();@p0 = 'Test Test Test T', @p1 = 'Test
Test '
NHibernate: SELECT authorizat_.TS as TS6_ FROM [dbo].
[AuthorizationLevels] authorizat_ WHERE authorizat_.
[authorizationleve...@p0;@p0 = 43
Test 'ItemBankCoreNH.UnitTests.AuthorizationLevelTests.Read' failed:
NHibernate.PropertyAccessException: Exception occurred getter of
ItemBankCoreNH.Base.EntityWithTypedId`1[[System.Int32, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Id
---> System.Reflection.TargetException: Object does not match target
type.
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object
target)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo
culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj,
Object[] index)
at
NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object
target)
--- End of inner exception stack trace ---
at
NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object
target)
at
NHibernate.Tuple.Entity.AbstractEntityTuplizer.GetIdentifier(Object
entity)
at
NHibernate.Persister.Entity.AbstractEntityPersister.GetIdentifier(Object
obj, EntityMode entityMode)
at
NHibernate.Persister.Entity.AbstractEntityPersister.IsTransient(Object
entity, ISessionImplementor session)
at NHibernate.Engine.ForeignKeys.IsTransient(String entityName,
Object entity, Nullable`1 assumed, ISessionImplementor session)
at
NHibernate.Event.Default.AbstractSaveEventListener.GetEntityState(Object
entity, String entityName, EntityEntry entry, ISessionImplementor
source)
at
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent
event)
at
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent
event)
at NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent
event)
at NHibernate.Impl.SessionImpl.SaveOrUpdate(String entityName,
Object obj)
at
NHibernate.Engine.CascadingAction.SaveUpdateCascadingAction.Cascade(IEventSource
session, Object child, String entityName, Object anything, Boolean
isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeToOne(Object child, IType type,
CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeAssociation(Object child, IType
type, CascadeStyle style, Object anything, Boolean
isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeProperty(Object child, IType
type, CascadeStyle style, Object anything, Boolean
isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeCollectionElements(Object
child, CollectionType collectionType, CascadeStyle style, IType
elemType, Object anything, Boolean isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeCollection(Object child,
CascadeStyle style, Object anything, CollectionType type)
at NHibernate.Engine.Cascade.CascadeAssociation(Object child, IType
type, CascadeStyle style, Object anything, Boolean
isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeProperty(Object child, IType
type, CascadeStyle style, Object anything, Boolean
isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeOn(IEntityPersister persister,
Object parent, Object anything)
at
NHibernate.Event.Default.AbstractSaveEventListener.CascadeAfterSave(IEventSource
source, IEntityPersister persister, Object entity, Object anything)
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.PerformSave(Object
entity, Object id, 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.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent
event)
at
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent
event)
at
NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent
event)
at
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent
event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at ItemBankCoreNH.Base.ManagerBase`2.Save(T entity) in C:\Visual
Studio Projects\ItemBankCoreNH\ItemBankCoreNH\Base\ManagerBase.cs:line
161
at ItemBankCoreNH.UnitTests.AuthorizationLevelTests.Read() in C:
\Visual Studio Projects\ItemBankCoreNH\ItemBankCoreNH\UnitTests
\AuthorizationLevelTests.cs:line 80
UnitTests\AuthorizationLevelTests.cs(88,0): at
ItemBankCoreNH.UnitTests.AuthorizationLevelTests.Read()
Here is the code for the test function. Error occurs
on .Save(entityA);
ItemBankCoreNH.BusinessObjects.AuthorizationLevel entity = new
ItemBankCoreNH.BusinessObjects.AuthorizationLevel();
entity.AuthorizationShortDesc = "Test Test Test T";
entity.AuthorizationDescription = "Test Test ";
//If I remove from here to the "END OF AUTHORIZATION" line everything
works fine.
IList<Authorization> entityAuth = new List<Authorization>();
Authorization a = new Authorization();
Authorization b = new Authorization();
a.UserName = "test123";
a.AuthorizationLevelID = 2;
b.UserName = "test123";
b.AuthorizationLevelID = 1;
entityAuth.Add(a);
entityAuth.Add(b);
entity.Authorizations = entityAuth;
//END OF AUTHORIZATION
manager.Save(entity);
Here is my HBM files for AuthorizationLevel and Authorization
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="ItemBankCoreNH.BusinessObjects" assembly="ItemBankCoreNH">
<class name="ItemBankCoreNH.BusinessObjects.Authorization,
ItemBankCoreNH" table="[dbo].[Authorization]"
optimistic-lock="dirty" dynamic-update="true" lazy="true">
<composite-id>
<key-property name="UserName">
<column name="[UserName]" sql-type="varchar" length="50" not-
null="true" />
</key-property>
<key-property name="AuthorizationLevelID">
<column name="[AuthorizationLevel]" sql-type="int" not-
null="true" />
</key-property>
</composite-id>
<version name="TS" generated="always" unsaved-value="null"
type="BinaryBlob">
<!-- Generated="always" is to have database handle generation of
timestamp -->
<column name="TS" not-null="false" sql-type="timestamp"/>
</version>
<!-- Leaving out fetch="join" does the default of a seperate
select statement, which is slower
http://stackoverflow.com/questions/1027019/what-is-the-difference-between-lazytrue-and-lazyproxy-in-nhibernate-->
<!-- insert and update = false because we have already defined
AuthorizationLevel as a key property and can only define it once.
https://forum.hibernate.org/viewtopic.php?p=2394781 -->
<!-- If we set Lazy="proxy" here it would do lazy loading, in this
case we really don't want to do it as we always want to know this
property value and will be
more performant then lazy loading in this case because there will
be less round trips to the database.
Many to one here means there are many authorizations to one
authorization level-->
<many-to-one name="AuthorizationLevel" class="AuthorizationLevel"
insert="false" update="false" fetch="join">
<column name="[AuthorizationLevel]" sql-type="int" />
</many-to-one>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="ItemBankCoreNH.BusinessObjects" assembly="ItemBankCoreNH">
<class name="ItemBankCoreNH.BusinessObjects.AuthorizationLevel,
ItemBankCoreNH" table="[dbo].[AuthorizationLevels]"
optimistic-lock="dirty" dynamic-update="true" lazy="true">
<id name="Id" column="[AuthorizationLevel]">
<generator class="native" />
</id>
<version name="TS" generated="always" unsaved-value="null"
type="BinaryBlob">
<!-- Generated="always" is to have database handle generation of
timestamp -->
<column name="TS" not-null="false" sql-type="timestamp"/>
</version>
<property name="AuthorizationShortDesc"
column="[AuthorizationShortDesc]" type="string" length="30" not-
null="true" />
<property name="AuthorizationDescription"
column="[AuthorizationDescription]" type="string" length="255" not-
null="true" />
<!-- nosetter.camelcase-underscore is required here due a a
LazyInitializationException when running the delete unit test.
See here for more detail:
http://kozmic.pl/archive/2009/05/14/two-way-one-to-many-associations-in-nhibernate.aspx
Casecade options are not all or all-delete-orphane due to failure
on delete unit test due to bidirectional references-->
<bag name="Authorizations" inverse="true" cascade="save-update"
access="nosetter.camelcase-underscore">
<key>
<column name="[AuthorizationLevel]" sql-type="int" />
</key>
<one-to-many class="AuthorizationLevel" />
</bag>
</class>
</hibernate-mapping>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.