We're newbies and have spent most of the day trying to figure this out.
The error is:
Hibernate.PropertyAccessException : Exception occurred getter of Fw.Domain.CmObject.Id
----> System.Reflection.TargetException : Object does not match target type.
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 2.0.1
Mapping documents:
Code:
<class name="CmObject" table="CmObject" discriminator-value="0">
<!--CmObject-->
<id name="Id">
<generator class="guid"/>
</id>
<discriminator column="Class$" type="Int64"></discriminator>
<property name="kflidCmObject_Guid" column="Guid$"></property>
...
The class:
Code:
...
public class CmObject
{
// TODO: Change this to something other than a guid.
public virtual Guid Id { get; set; }
...
}
The repository:
Code:
namespace Fw.Repositories
{
public class CmObjectRepository : ICmObjectRepository
{
public void Add(CmObject cmObject)
{
using (ISession session = NHibernateHelper.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(cmObject);
transaction.Commit();
}
}
}
}
The unit test:
Code:
private void CreateInitialData()
{
using (ISession session = _sessionFactory.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
foreach (CmObject obj in _obj)
session.Save(obj);
transaction.Commit();
}
}
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
_configuration = new Configuration();
_configuration.Configure();
_configuration.AddAssembly("FDO");
_sessionFactory = _configuration.BuildSessionFactory();
}
[SetUp]
public void SetupContext()
{
new SchemaExport(_configuration).Execute(false, true, false, false);
this.CreateInitialData();
}
[Test]
public void Can_add_new_product()
{
//CmPossibilityList cmPossList = new CmPossibilityList();
CmObject cmObj = new CmObject();
ICmObjectRepository repository = new CmObjectRepository();
repository.Add(cmObj);
// use session to try to load the product
using (ISession session = _sessionFactory.OpenSession())
{
CmObject fromDb = session.Get<CmObject>(cmObj.Id);
// Test that the product was successfully inserted
Assert.IsNotNull(fromDb);
Assert.AreNotSame(cmObj, fromDb);
Assert.AreEqual(cmObj.kflidCmObject_Class, fromDb.kflidCmObject_Class);
Assert.AreEqual(cmObj.kflidCmObject_Owner, fromDb.kflidCmObject_Owner);
Assert.AreEqual(cmObj.kflidCmObject_OwnFlid, fromDb.kflidCmObject_OwnFlid);
}
}
Full stack trace of any exception that occurs:
------ Test started: Assembly: TestNHibernate.dll ------
TestCase 'CmObjectRepository_Fixture.Can_add_new_product' failed: TestFixtureSetUp failed in CmObjectRepository_Fixture
TestFixture failed: NHibernate.PropertyAccessException : Exception occurred getter of Fw.Domain.CmObject.Id
----> System.Reflection.TargetException : Object does not match target type.
at NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object target)
at NHibernate.Engine.UnsavedValueFactory.GetUnsavedIdentifierValue(String unsavedValue, IGetter identifierGetter, IType identifierType, ConstructorInfo constructor)
at NHibernate.Tuple.PropertyFactory.BuildIdentifierProperty(PersistentClass mappedEntity, IIdentifierGenerator generator)
at NHibernate.Tuple.Entity.EntityMetamodel..ctor(PersistentClass persistentClass, ISessionFactoryImplementor sessionFactory)
at NHibernate.Persister.Entity.AbstractEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory)
at NHibernate.Persister.Entity.SingleTableEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping)
at NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping cfg)
at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
at NHibernate.Cfg.Configuration.BuildSessionFactory()
at CmObjectRepository_Fixture.TestFixtureSetUp() in C:\FwHibernate\Src\FDO\TestNHibernate\TestCmObject.cs:line 69
--TargetException
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)
0 passed, 1 failed, 0 skipped, took 2.20 seconds.