Hy !
Im new in nhibernate.
I get a simple table shecma in postgresql database:
CREATE TABLE users ( usid SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, surn VARCHAR(50) NOT NULL, logn VARCHAR(3) NOT NULL UNIQUE, pass VARCHAR(50) NOT NULL, mail VARCHAR(100) NOT NULL, extn VARCHAR(3) NOT NULL UNIQUE, ujid VARCHAR(100) NOT NULL UNIQUE, enab BOOLEAN NOT NULL DEFAULT FALSE, prid INTEGER NOT NULL REFERENCES users ON UPDATE CASCADE ON DELETE CASCADE, crdt TIMESTAMP NOT NULL DEFAULT now() ); CREATE INDEX users_name_idx ON users (name); CREATE INDEX users_surn_idx ON users (surn); CREATE INDEX users_enab_idx ON users (ujid);
I define a class in .Net to map this table:
namespace DataObjects.Domain { public class User { public virtual long Usid { get; set; } public virtual string Name { get; set; } public virtual string Surn { get; set; } public virtual string Logn { get; set; } public virtual string Pass { get; set; } public virtual string Mail { get; set; } public virtual string Extn { get; set; } public virtual string Ujid { get; set; } public virtual bool Enab { get; set; } public virtual long Prid { get; set; } public virtual DateTime Crdt { get; set; } } }
and then, i create a mapping file:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataObjects" namespace="DataObjects.Domain"> <class name="DataObjects.Domain.User, DataObjects" table ="users"> <id name="Usid" column="usid" type="long" unsaved-value="0"> <generator class="native"> <param name="sequense">users_usid_seq</param> </generator> </id> <timestamp name="Crdt" column="crdt" generated="always"></timestamp> <property name="Name" column="name" type="String" length="50" not-null="true"></property> <property name="Surn" column="surn" type="String" length="50" not-null="true"></property> <property name="Logn" column="logn" type="String" length="3" not-null="true" unique="true"></property> <property name="Pass" column="pass" type="String" length="50" not-null="true"></property> <property name="Mail" column="mail" type="String" length="100" not-null="true"></property> <property name="Extn" column="extn" type="String" length="3" not-null="true" unique="true"></property> <property name="Ujid" column="ujid" type="String" length="100" not-null="true" unique="true"></property> <property name="Enab" column="enab" type="Boolean" not-null="true" generated="insert"></property> <property name="Prid" column="prid" type="long" not-null="true"></property> </class> </hibernate-mapping>
This work ok, but the field "prid" is a foreing key from the same user table, so, it should by defined in mapping as a many-to-one element, like this:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataObjects" namespace="DataObjects.Domain"> <class name="DataObjects.Domain.User, DataObjects" table ="users"> <id name="Usid" column="usid" type="long" unsaved-value="0"> <generator class="native"> <param name="sequense">users_usid_seq</param> </generator> </id> <timestamp name="Crdt" column="crdt" generated="always"></timestamp> <property name="Name" column="name" type="String" length="50" not-null="true"></property> <property name="Surn" column="surn" type="String" length="50" not-null="true"></property> <property name="Logn" column="logn" type="String" length="3" not-null="true" unique="true"></property> <property name="Pass" column="pass" type="String" length="50" not-null="true"></property> <property name="Mail" column="mail" type="String" length="100" not-null="true"></property> <property name="Extn" column="extn" type="String" length="3" not-null="true" unique="true"></property> <property name="Ujid" column="ujid" type="String" length="100" not-null="true" unique="true"></property> <property name="Enab" column="enab" type="Boolean" not-null="true" generated="insert"></property> <many-to-one name="Prid" column="prid" class="DataObjects.Domain.User, DataObjects" cascade="all"></many-to-one> </class> </hibernate-mapping>
but now, with this ideal mapping file, when i use the object, nhibernate generate this error:
DataAccessTest.CanGetUserById : FailedNHibernate: SELECT user0_.usid as usid1_0_, user0_.crdt as crdt1_0_, user0_.name as name1_0_, user0_.surn as surn1_0_, user0_.logn as logn1_0_, user0_.pass as pass1_0_, user0_.mail as mail1_0_, user0_.extn as extn1_0_, user0_.ujid as ujid1_0_, user0_.enab as enab1_0_, user0_.prid as prid1_0_ FROM users user0_ WHERE user0_.usid=:p0; :p0 = '0'
System.InvalidCastException: Specified cast is not valid. at (Object , Object[] , SetterCallback ) at NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(Object target, Object[] values) at NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values) NHibernate.MappingException: Invalid mapping information specified for type DataObjects.Domain.User, check your mapping file for property type mismatches at NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values) at NHibernate.Impl.SessionImpl.InitializeEntity(Object obj) at NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session) at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) at NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, Type optionalEntityName, Object optionalIdentifier, IEntityPersister persister) at NHibernate.Loader.Entity.AbstractEntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject, Object optionalId) at NHibernate.Loader.Entity.AbstractEntityLoader.Load(Object id, Object optionalObject, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Load(Object id, Object optionalObject, LockMode lockMode, ISessionImplementor session) at NHibernate.Impl.SessionImpl.DoLoad(Type theClass, Object id, Object optionalObject, LockMode lockMode, Boolean checkDeleted) at NHibernate.Impl.SessionImpl.DoLoadByClass(Type clazz, Object id, Boolean checkDeleted, Boolean allowProxyCreation) at NHibernate.Impl.SessionImpl.Get(Type clazz, Object id) at NHibernate.Impl.SessionImpl.Get<T>(Object id) at DataAccess.DataProvider.GetUserById(Int64 usid) in DataProvider.cs: line 73 at DataAccessTest.DataAccessTest.CanGetUserById() in DataAccessTest.cs: line 124
some one have any idea of what is wrong there????
Thanks in advance !!!
|