-->
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.  [ 7 posts ] 
Author Message
 Post subject: Mapping Problem
PostPosted: Sat May 16, 2009 3:09 pm 
Newbie

Joined: Sat May 16, 2009 1:51 pm
Posts: 4
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 !!!


Top
 Profile  
 
 Post subject: Re: Mapping Problem
PostPosted: Mon May 25, 2009 9:13 am 
Hi there.

Have you look on return type of your object?
Maybe it's returning a proxy type, not an User. Then you get a ClassCastException
Try using lazy="false" on your <many-to-one> mapping.

[]


Top
  
 
 Post subject: Re: Mapping Problem
PostPosted: Tue May 26, 2009 1:32 am 
Newbie

Joined: Sat May 16, 2009 1:51 pm
Posts: 4
Hi Luiz

This aproach work fine for me!!

Thanks a lot !

Danyel


Top
 Profile  
 
 Post subject: Re: Mapping Problem
PostPosted: Tue May 26, 2009 7:12 am 
Glad to help.


Top
  
 
 Post subject: Re: Mapping Problem
PostPosted: Thu May 28, 2009 1:36 am 
Newbie

Joined: Sat May 16, 2009 1:51 pm
Posts: 4
Hi again Luiz

sadly, the solution only work partially.

I run some test to prove it, but only the test that return a user by id work fine. Another simple test, like return a user by first name, ends with the same error :

System.InvalidCastException: Specified cast is not valid.
NHibernate.MappingException: Invalid mapping information specified for type DataObjects.Domain.User, check your mapping file for property type mismatches

The 2 SQLs that hibernate generate are:

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'


select user0_.usid as usid1_, user0_.crdt as crdt1_, user0_.name as name1_, user0_.surn as surn1_, user0_.logn as logn1_, user0_.pass as pass1_, user0_.mail as mail1_, user0_.extn as extn1_, user0_.ujid as ujid1_, user0_.enab as enab1_, user0_.prid as prid1_ from users user0_ where (user0_.name=:p0 ); :p0 = 'System'

Apparently the sql is very similar. Some new ideas?

Thanks for help.

Danyel.


Top
 Profile  
 
 Post subject: Re: Mapping Problem
PostPosted: Thu May 28, 2009 2:30 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
When you map prid as a many-to-one to user, hibernate returns a object of type user, not a long. You have to change your property mapping for prid from long to user.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Mapping Problem
PostPosted: Fri May 29, 2009 3:05 pm 
Newbie

Joined: Sat May 16, 2009 1:51 pm
Posts: 4
Hi wolli

Your answer hit the problem !!

Thanks a lot.

This 2 answers resolv my problem and now i can run all my test succefull.

Danyel.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.