Cannot mapping collection by property-ref

Description

I'm receive the following exception if I map collection by property-ref and it need to be created for more than one object:

NHibernate.LazyInitializationException : Initializing[NHibernate.Test.NHSpecificTest.NH3557.Person#5]-failed to lazily initialize a collection of role: NHibernate.Test.NHSpecificTest.NH3557.Person.Addresses, no session or session was closed

Environment

None

Attachments

1
  • 25 Oct 2013, 09:30 AM

Activity

Show:

Daniel A. Schilling October 30, 2013 at 2:14 PM
Edited

From the many-to-one documentation http://nhforge.org/doc/nh/en/index.html#mapping-declaration-manytoone (you're doing this on a one-to-many, but the same property-ref rules apply):

The property-ref attribute should only be used for mapping legacy data where a foreign key refers to a unique key of the associated table other than the primary key.

(emphasis is mine)

If you are going to use RelatedPersonId as a property-ref, there should be a unique constraint on that property, which would prevent you from inserting two people who both have RelatedPersonId == 5. I'm guessing this isn't what you actually want to do, though. Just a shot in the dark, I think you probably want a many-to-one on Person named RelatedPerson, and also a one-to-many on Person named Addresses that simply points to Person.Id, not using property-ref. Then you would get the related person addresses via person1.RelatedPerson.Addresses.

SR October 25, 2013 at 9:30 AM

Test case

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?

Created October 25, 2013 at 9:28 AM
Updated November 4, 2013 at 3:33 AM
Who's Looking?

Flag notifications