-->
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: MappingException when upgrading from 0.9.1.0 to 1.0.1
PostPosted: Sat Nov 19, 2005 1:11 pm 
Newbie

Joined: Sat Nov 19, 2005 1:01 pm
Posts: 9
Hi.

I've got an app that works just fine with NHibernate version 0.9.1.0. This morning I decided to try and upgrade to version 1.0.1. I downloaded the zip file from sourceforge, updated my references to point to the new bin directory and then rebuilt and tried to run the app.

When I do I get the following error:

NHibernate.MappingException: Repated column in mapping for class xxx.xxx.User should be mapped with insert="false" update="false": UserID

If I go back to the 0.9.1.0 binaries the app still runs fine. My mapping file is as follows (namespaces have been changed to protect the innocent):

Code:
<?xml version='1.0' encoding='utf-8' ?>
<hibernate-mapping xmlns='urn:nhibernate-mapping-2.0'>

   <class name='xxx.xxx.User,xxx.xxx' table='Users'>

      <id name='UserID' column='UserID' type='Int32' unsaved-value='0'>
         <generator class='native' />
      </id>

      <property name='Username' column='Username' type='String' />

      <property name='Password' column='Password' type='String' />

      <property name='FirstName' column='FirstName' type='String' />

      <property name='LastName' column='LastName' type='String' />

      <many-to-one
          name='UserType'
          class='xxx.xxx.UserType,xxx.xxx'
          column='UserID'
          not-null='true' />
         
      <set
         name='UserEstablishments'
         lazy='true'
         inverse='true'
         cascade='all-delete-orphan'>
         <key
            column='UserID' />
         <one-to-many
            class='xxx.xxx.UserEstablishment,xxx.xxx' />
      </set>

   </class>

</hibernate-mapping>


As you can see, the supposedly repeated column mapping is not repeatedly mapped nor is it a regular property; it's my identifier. As such I don't think I can set the insert or update properties to false as suggested by the error since I don't believe identifiers even support those properties.

Any ideas on what I'm doing wrong?

Thanks.


Top
 Profile  
 
 Post subject: Figured it out, but why?
PostPosted: Sat Nov 19, 2005 1:19 pm 
Newbie

Joined: Sat Nov 19, 2005 1:01 pm
Posts: 9
Well, I figured out that the problem is apparently on my many-to-one mapping. By adding insert and update = false to the UserType many-to-one I got the app to work.

But why do I need to do this with the new version? I mean, this isn't the only many-to-one mapping in my application but it appears to be the only one with this requirement.

It's good to get the app working again but even better to understand why.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 7:05 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Because if UserID is set to 1, but UserType is set to a type with ID 2, it would make NH confused about which value it should store into the UserID column.

And by the way, the mapping looks more like a <one-to-one> to me, are you sure you really wanted <many-to-one> here?


Top
 Profile  
 
 Post subject: Hmmm
PostPosted: Sat Nov 19, 2005 7:37 pm 
Newbie

Joined: Sat Nov 19, 2005 1:01 pm
Posts: 9
I think it is many-to-one and I think a problem in the code I posted might have led you to think otherwise. The mapping for UserType's column was supposed to be 'UserTypeID' rather than UserID as I posted. I must've changed it while screwing around trying to get things working.

But you know, I think this just points up a bigger problem I'm having which is figuring out one-to-one relations. When to use them and how.

In this case my db structure is:

User
====
UserID bigint identity primary key,
UserTypeID int foreign key references UserType.UserTypeID
.
.
.

UserType
======
UserTypeID int identity primary key,
.
.
.

So each user has one UserType and each UserType can have 0-n Users. But when it comes to hibernate mapping strategies I get confused. I always have a hard time understanding for instance, in a many-to-one, which class is the many and which is the one? The one I'm mapping or the one I'm mapping to?

Appreciate any help anyone can provide getting my head around these concepts.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 20, 2005 7:12 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Basically, many-to-one is a reference to one object and one-to-many is a collection, i.e. a reference to many objects.

If the column names are different, your mapping should work without insert=false and update=false. Please check again and post the right mapping file here if you still have problems.


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.