Hello I'am a newbie in NHibernate. Can anyone tell me why it throws up a exception? Files hibernate.hbm.xml and document.hbm.xml work as embedded resource and always copy to output directory, so I don't get it why it's not working?
GenericADOException
could not load an entity: [Model.Document#1][SQL: SELECT document0_.DocumentID as DocumentID0_0_, document0_.Title as Title0_0_ FROM Document document0_ WHERE document0_.DocumentID=?]
Hibernate version: 2.0
Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model">
<class name="Model.Document, Model" table="Document">
<id name="DocumentID" column="DocumentID" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Title" column="Title" type="string" length="50" />
</class>
</hibernate-mapping>
hibernate.cfg.xml:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">Data Source=VIRUS;Initial Catalog=AdventureWorks;Integrated Security=True</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="show_sql">true</property>
<mapping assembly="Model" />
</session-factory>
</hibernate-configuration>
Code between sessionFactory.openSession() and session.close():Code:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using Model;
using NHibernate.Criterion;
namespace Controler.Repositories
{
public class DocumentRepository
{
/// <summary>
///
/// </summary>
/// <param name="CategoryId"></param>
/// <returns></returns>
public static Document GetById(int DocumentID)
{
Document retObj = null;
using (ISession session = NHibernateManager.OpenSession())
{
retObj = (Document)session.Get<Document>(DocumentID); // <-is where the exception occurs
}
return retObj;
}
}
}
Full stack trace of any exception that occurs: w NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, String optionalEntityName, Object optionalIdentifier, IEntityPersister persister)
w NHibernate.Loader.Entity.AbstractEntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject, Object optionalId)
w NHibernate.Loader.Entity.AbstractEntityLoader.Load(Object id, Object optionalObject, ISessionImplementor session)
w NHibernate.Persister.Entity.AbstractEntityPersister.Load(Object id, Object optionalObject, LockMode lockMode, ISessionImplementor session)
w NHibernate.Event.Default.DefaultLoadEventListener.LoadFromDatasource(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
w NHibernate.Event.Default.DefaultLoadEventListener.DoLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
w NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
w NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
w NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
w NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
w NHibernate.Impl.SessionImpl.Get(String entityName, Object id)
w NHibernate.Impl.SessionImpl.Get(Type entityClass, Object id)
w NHibernate.Impl.SessionImpl.Get[T](Object id)
w Controler.Repositories.DocumentRepository.GetById(Int32 DocumentID) w C:\Documents and Settings\Admin\Moje dokumenty\Visual Studio 2005\Projects\NHibernate2\Controller\DocumentRepository.cs:wiersz 68
w NHibernateTest.Program.Main(String[] args) w C:\Documents and Settings\Admin\Moje dokumenty\Visual Studio 2005\Projects\NHibernate2\ConsoleView\Program.cs:wiersz 18
w System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
w System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
w Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
w System.Threading.ThreadHelper.ThreadStart()
Name and version of the database you are using:
MSSQL2005The generated SQL (show_sql=true):
SELECT document0_.DocumentID as DocumentID0_0_, document0_.Title as Title0_0_ FROM Document document0_ WHERE document0_.DocumentID=?
Class that is being mapped:
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace Model
{
public class Document
{
private int _documentID;
private string _title;
private string _filename;
private string _fileextension;
private string _revision;
private int _changenumber;
private int _status;
private string _documentsummary;
private string _document;
private DateTime _modifiedDate;
public virtual int DocumentID
{
get { return _documentID; }
set { _documentID = value; }
}
public virtual string Title
{
get { return _title; }
set { _title = value; }
}
public virtual string FileName
{
get { return _filename; }
set { _filename = value; }
}
public virtual string FileExtension
{
get { return _fileextension; }
set { _fileextension = value; }
}
public virtual string Revision
{
get { return _revision; }
set { _revision = value; }
}
public virtual int ChangeNumber
{
get { return _changenumber; }
set { _changenumber = value; }
}
public virtual int Status
{
get { return _status; }
set { _status = value; }
}
public virtual string DocumentSummary
{
get { return _documentsummary; }
set { _documentsummary = value; }
}
public virtual string DocumentContent
{
get { return _document; }
set { _document = value; }
}
public virtual DateTime ModifiedDate
{
get { return _modifiedDate; }
set { _modifiedDate = value; }
}
}
}