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.  [ 11 posts ] 
Author Message
 Post subject: Mapping aggregate with composite key fields
PostPosted: Mon Jun 19, 2006 6:45 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
First off, I'm using NHibernate 1.2.0 and MSSQL with a schema that cannot be changed.

I have a class called RoleAssignment, who's key is a composite consisting of a User object and a Role object. This maps to a shared primary key in the database.

The RoleAssignment class has a list of CompositeConstraint objects. That class has a Guid primary key, but I reference the rows by LoginId (user's PK) and RoleId (role's PK)

I'm not exactly sure how to map this. My bag looks like this:

Code:
<bag name="constraints" access="NHibernate.Generics.GenericAccessor, NHibernate.Generics" cascade="all-delete-orphan">
      <key>
        <column name="RoleId"/>
        <column name="LoginId"/>       
      </key>     
      <one-to-many class="CompositeConstraint"/>
    </bag>


but I'm not sure how I specify that the RoleId and LoginId columns come from the classes that make up the composite key of this class.

Any help would be greatly appreciated.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 8:23 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
Should I change the composite-key to containt key columns, rather than <key-many-to-one>s? Or will NHibernate know to map the primary key of the composite key classes and use those for the bag reference above?

Any help is greatly appreciated.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 12:48 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
I did some more research, and I'm getting a LazyInitialize exception even though the collection isn't lazy. This happens whether I have <key-property> or <key-many-to-one> for the <composite-id> element.

I was able to work around the problem by adding a surrogate key to the database and mapping a standard <id> to that, but I don't want to introduce schema changes, as this is a legacy system in use.

Why would I get a "Failed to lazily initialize a collection - no session" error when the collection isn't lazy? The only thing is that both classes have composite keys.

Any help here would be greatly appreciated. Thanks.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 4:05 pm 
Newbie

Joined: Mon May 22, 2006 12:22 pm
Posts: 12
You didn't specify lazy="false" for your bag

Code:
<bag name="constraints" access="NHibernate.Generics.GenericAccessor, NHibernate.Generics" cascade="all-delete-orphan">
      <key>
        <column name="RoleId"/>
        <column name="LoginId"/>       
      </key>     
      <one-to-many class="CompositeConstraint"/>
    </bag> 


Last edited by rich_out on Tue Jun 20, 2006 4:33 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 4:16 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
Yes you're right, but I get the same results even if I do specify it. Isn't Lazy=false the default?

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 4:36 pm 
Newbie

Joined: Mon May 22, 2006 12:22 pm
Posts: 12
The documentation says it is, but I've found explicitly declaring it forces lazy loading not to happen. Are you serializing your containing object?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 4:41 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
No. I have a test that saves the parent object to a database (with child objects).

It then reloads it from the database and checks if the child objects got loaded as well. The error happens at that last step, when accessing the collection.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 2:13 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
subdigital wrote:
Isn't Lazy=false the default?


According to http://forum.hibernate.org/viewtopic.php?t=960004,
starting with 1.2.0Alpha1, the default is lazy=true

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 8:56 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
I'm using 1.2.0, so I think it's ok.

Besides I've been saving/loading collections the same way (not specifying lazy) and it works for all of my other mappings.

Good to know that lazy=true is the new default.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 9:01 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
update: I decided to move forward with this issue and add a surrogate key to the 2 tables in question, and now it's working, but this is on my development database. Migrating these changes to production is going to be a pain to explain and carry out.

Can anyone find a reason why this isn't working? Am I mapping it correctly?

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 6:33 am 
Beginner
Beginner

Joined: Fri Jul 29, 2005 10:34 am
Posts: 25
subdigital wrote:
update: I decided to move forward with this issue and add a surrogate key to the 2 tables in question, and now it's working, but this is on my development database. Migrating these changes to production is going to be a pain to explain and carry out.

Can anyone find a reason why this isn't working? Am I mapping it correctly?


Hi,

I think I've run into your same problem:
http://forum.hibernate.org/viewtopic.php?t=964255

From what i've found so far looks like a restriction. You can make a 1:many association if the child it's not related using the Primary Key.

Sebastian


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