These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Wrong mapping file?
PostPosted: Mon Nov 17, 2008 7:15 am 
Newbie

Joined: Mon Nov 17, 2008 6:42 am
Posts: 6
Dear Nh users,
this is my first post on your forum. I try to use NHibernate with my project, but I cannot configure right the mapping. I try the examples on the web and they work well.

Hibernate version: 2.0.1

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

<class name="Utente" table="dbo.UTENTI" lazy="true">
<id name="codice" unsaved-value="null" access="field" type="System.String" length="15">
<column name="CODICE"/>
<generator class="assigned"></generator>
</id>

<property name="Descrizione" column="DESCRIZIONE" type="System.String" />
<property name="PasswordMD5" column="PASSWDMD5" type="System.String" />
<property name="Tipologia" column="TIPOLOGIA" type="System.Int32" />
<property name="Codtec" column="CODTEC" type="System.String" />
<property name="Settore1" column="SETTORE" type="System.String" />
<property name="Settore2" column="SETTORE2" type="System.String" />
<property name="DoSync" column="DOSYNC" type="System.String" />
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
s.Get<Domain.Entities.Utente>("ELE");

Full stack trace of any exception that occurs:
NHibernate.PropertyAccessException non รจ stata gestita
Message="Invalid Cast (check your mapping for property type mismatches); setter of Domain.Entities.Utente"
Source="NHibernate"
StackTrace:
in NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Tuple\Entity\PocoEntityTuplizer.cs:riga 275
in NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValues(Object entity, Object[] values) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Tuple\Entity\PocoEntityTuplizer.cs:riga 259
in NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values, EntityMode entityMode) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:riga 3720
in NHibernate.Engine.TwoPhaseLoad.InitializeEntity(Object entity, Boolean readOnly, ISessionImplementor session, PreLoadEvent preLoadEvent, PostLoadEvent postLoadEvent) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Engine\TwoPhaseLoad.cs:riga 87
in NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session, Boolean readOnly) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Loader\Loader.cs:riga 617
in NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Loader\Loader.cs:riga 466
in NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Loader\Loader.cs:riga 187
in NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, String optionalEntityName, Object optionalIdentifier, IEntityPersister persister) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Loader\Loader.cs:riga 1487
in NHibernate.Loader.Entity.AbstractEntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject, Object optionalId) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Loader\Entity\AbstractEntityLoader.cs:riga 43
in NHibernate.Loader.Entity.AbstractEntityLoader.Load(Object id, Object optionalObject, ISessionImplementor session) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Loader\Entity\AbstractEntityLoader.cs:riga 38
in NHibernate.Persister.Entity.AbstractEntityPersister.Load(Object id, Object optionalObject, LockMode lockMode, ISessionImplementor session) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:riga 3356
in NHibernate.Event.Default.DefaultLoadEventListener.LoadFromDatasource(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Event\Default\DefaultLoadEventListener.cs:riga 331
in NHibernate.Event.Default.DefaultLoadEventListener.DoLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Event\Default\DefaultLoadEventListener.cs:riga 317
in NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Event\Default\DefaultLoadEventListener.cs:riga 102
in NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Event\Default\DefaultLoadEventListener.cs:riga 158
in NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Event\Default\DefaultLoadEventListener.cs:riga 85
in NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Impl\SessionImpl.cs:riga 2055
in NHibernate.Impl.SessionImpl.Get(String entityName, Object id) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Impl\SessionImpl.cs:riga 1030
in NHibernate.Impl.SessionImpl.Get(Type entityClass, Object id) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Impl\SessionImpl.cs:riga 1021
in NHibernate.Impl.SessionImpl.Get[T](Object id) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Impl\SessionImpl.cs:riga 986
in nhTest.Program.Main(String[] args) in D:\CentroComputer\GestioneOreSvn\nhTest\Program.cs:riga 14
in System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
in System.Threading.ThreadHelper.ThreadStart()
InnerException: System.InvalidCastException
Message="Cast specificato non valido."
Source="Domain"
StackTrace:
in (Object , Object[] , SetterCallback )
in NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(Object target, Object[] values) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Bytecode\Lightweight\AccessOptimizer.cs:riga 32
in NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) in c:\CSharp\NH2.0.x\nhibernate\src\NHibernate\Tuple\Entity\PocoEntityTuplizer.cs:riga 271
InnerException:


Name and version of the database you are using: MSSql Server 2005

The generated SQL (show_sql=true):
NHibernate: SELECT utente0_.CODICE as CODICE0_0_, utente0_.DESCRIZIONE as DESCRI
ZI2_0_0_, utente0_.PASSWDMD5 as PASSWDMD3_0_0_, utente0_.TIPOLOGIA as TIPOLOGIA0
_0_, utente0_.CODTEC as CODTEC0_0_, utente0_.SETTORE as SETTORE0_0_, utente0_.SE
TTORE2 as SETTORE7_0_0_, utente0_.DOSYNC as DOSYNC0_0_ FROM dbo.UTENTI utente0_
WHERE utente0_.CODICE=@p0; @p0 = 'ELE'


Debug level Hibernate log excerpt:



This is my Database schema

CREATE TABLE [dbo].[UTENTI](
[CODICE] [char](15) NOT NULL,
[DESCRIZIONE] [varchar](50) NOT NULL,
[PASSWDMD5] [varchar](50) NOT NULL,
[TIPOLOGIA] [int] NOT NULL,
[CODTEC] [char](10) NULL,
[SETTORE] [char](2) NULL,
[SETTORE2] [char](2) NULL,
[DOSYNC] [char](1) NOT NULL,
CONSTRAINT [PK_UTENTI] PRIMARY KEY CLUSTERED
(
[CODICE] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]




Probably I made some mistake in the mapping file... can you suggest me some tutorial for Nh2.0 for undestand better the mapping?

It I try to Add a new "Utente" object with SaveOrUpdate method the command execute successfully, but nothing where stored into database...

Best regards

Claudio Cas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2008 8:49 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
Message="Invalid Cast (check your mapping for property type mismatches); setter of Domain.Entities.Utente"


Sounds like you have a type mismatch between one of your properties and the corresponding mapping. Can you post the class ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2008 9:40 am 
Newbie

Joined: Mon Nov 17, 2008 6:42 am
Posts: 6
Dear Wolli
thanks for you suggestion: I use char type for DoSync and seems it was the problem. Now I use string and get and saveorupdate works great!
Thanks a lot for your help!

Can you suggest me some tutorial or books for understand better NHibernate?

thanks a lot!
ClaudioCas


Top
 Profile  
 
 Post subject: Still in troubles...
PostPosted: Mon Nov 17, 2008 11:02 am 
Newbie

Joined: Mon Nov 17, 2008 6:42 am
Posts: 6
Dear wolly (and the others :D)
I was happy too early... The get works, but the Save give me this error Unexpected row count: 0; expected: 1

This is my class...

public class Utente
{
private string codice;

public virtual string Codice
{
get { return codice; }
set { codice = value; }
}

private string descrizione;

public virtual string Descrizione
{
get { return descrizione; }
set { descrizione = value; }
}
private string passwordMD5;

public virtual string PasswordMD5
{
get { return passwordMD5; }
set { passwordMD5 = value; }
}

private int tipologia;

public virtual int Tipologia
{
get { return tipologia; }
set { tipologia = value; }
}
private string codtec;

public virtual string Codtec
{
get { return codtec; }
set { codtec = value; }
}
private string settore;

public virtual string Settore1
{
get { return settore; }
set { settore = value; }
}
private string settore2;

public virtual string Settore2
{
get { return settore2; }
set { settore2 = value; }
}
private string dosync;

public virtual string DoSync
{
get { return dosync; }
set { dosync = value; }
}
}
}


and this is my mapping
<class name="Utente" table="dbo.UTENTI" lazy="true">

<id name="codice" unsaved-value="null" access="field" type="System.String" length="15">
<column name="CODICE"/>
<generator class="assigned" />
</id>

<property name="Descrizione" column="DESCRIZIONE" type="System.String" />
<property name="PasswordMD5" column="PASSWDMD5" type="System.String" />
<property name="Tipologia" column="TIPOLOGIA" type="System.Int32" />
<property name="Codtec" column="CODTEC" type="System.String" />
<property name="Settore1" column="SETTORE" type="System.String" />
<property name="Settore2" column="SETTORE2" type="System.String" />
<property name="DoSync" column="DOSYNC" type="System.String" />
</class>
</hibernate-mapping>


OK :D Probably I am too stupid for use NHibernate. I called the SaveOrUpdate method on new object where I modify the CODICE (like id) manually and so NHibernate doesn't find the previus row in the database...
I should use Save(obj,id) and it works.

Thanks a lot for your suggestion...
BTW: Why in the main Nhibernate page still say that the last version available is 1.2 instead 2.0??


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2008 3:40 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
When you use "assigned" id's you have to explicitly Save() or Update() because hibernate can't tell if the object is new or just has changed.

have a look at http://www.nhforge.org. It's now has become the official website.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.