-->
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.  [ 4 posts ] 
Author Message
 Post subject: NHibernate Mapping problems
PostPosted: Tue Mar 27, 2007 6:43 am 
Newbie

Joined: Tue Mar 27, 2007 6:30 am
Posts: 12
Hi,
I'm new to NHibernate and am having difficulty getting it to work.

the following code causes the Mapping Exception "Duplicate class/entity mapping User";

string AssemblyName;
config = new Configuration();
AssemblyName=Assembly.GetExecutingAssembly().GetName().Name);
config.AddAssembly(AssemblyName);
config.AddClass(typeof(QuickStart.User));

The assembly is called quickstart and I have one class named User.

If I remove the config.AddClass statement I get the error:

"Unknown entity class: QuickStart.User"

when executing the session.Save(us) in the following code;

User us = new User();
...set properties before saving
ISessionFactory dal = new Configuration().BuildSessionFactory();
ISession session=dal.OpenSession();

ITransaction trans = session.BeginTransaction();
session.Save(us);

I have set the build action on the mapping file to embedded so thought the problem was not registering the classes at the start.

can anyone help with this as I am out of ideas.

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 12:03 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
post your class and mapping file. it makes sense that you are getting a duplicate class error when adding the assembly and then adding the class explicitly.

-devon


Top
 Profile  
 
 Post subject: duplicate
PostPosted: Tue Mar 27, 2007 12:09 pm 
Newbie

Joined: Tue Mar 27, 2007 6:30 am
Posts: 12
Thanks,
in the interim I have sorted the problem. As you say, the duplicate makes sense, so I removed it.

After some digging around it seems I needed to make the properties on my class virtual.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 2:31 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
yes, if the class is to be loaded lazily (through an association) then the methods must be virtual. classes are configured for lazily loading in two ways:

1) the association has a lazy="true" attribute:

Code:
<bag name="collection" lazy="true" ...>
  ...
</bag>


2) the class mapping file itself has a lazy="true"

Code:
<hibernate-mapping
   xmlns="urn:nhibernate-mapping-2.0"
   namespace="Your.Assembly.Namespace"
   assembly="Your.Assembly">

   <class
      name="ClassName"
      lazy="true"
      ... >

      ...
   </class>

</hibernate-mapping>


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