4

I have 2 entities Person and Address, Person has one Address.

EDIT: The Address already exists, Im just wanting to save the foreign key.

When I do this:

  PersonDTO person = new PersonDTO();
    person.Age = "Bob";
    person.Address = new AddressDTO {Key = 123};
    Save(person);

I get this exception:

Cannot insert the value NULL into column 'Key', table 'Address'; column does not allow nulls. INSERT fails.The statement has been terminated.

Mapping file fragment from Person

<class name="PersonDTO" table="Person" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Key" column="PersonKey" type="Guid">
      <generator class="guid" />
    </id>
    <one-to-one name="Address" class="AddressDTOl" />
  </class>

I don't understand why this happens, im giving Address Key a value. Is my approach flawed?

1
  • Include NHibernate mappings and class declarations. Commented Feb 26, 2009 at 12:58

3 Answers 3

2

You need to do this

AddressDTO add = new AddressDTO {Key = 123};
Save(add);

PersonDTO person = new PersonDTO();
person.Age = "Bob";
person.Address = add;
Save(person);

Or modify your mapping if you don't want to explicitly save Address :

<many-to-one name="Address" column="..." class="AddressDTO" cascade="save-update" />

If the address already exists, you need to get it from database :

PersonDTO person = new PersonDTO();
person.Age = "Bob";
person.Address = GetAddressDTO( 123 );
Save(person);
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I wasnt clear the address already exists, I just want to save Person's link to an address
0

You should save the address before saving the person. Depending on the generator you may have to use the save overload that passes in the ID.

If you need the save to be implicit you should set the cascade of address property in the person.

3 Comments

Sorry I wasnt clear the address already exists, I just want to save Person's link to an address.
Did you get the address with the same session that you are using to do the save? If not you need to join the address to the saving session.
? Im not getting the address, in the example above I instantiate a new address with a know key. I also switched it so that it gets the full address object from the same session but i get the same error.
0

Fixed it!

I changed my fluent nhibernate mapping, I used a References Relationship instead of HasOne.

This resulted in changing the mapping to this:

<many-to-one name="Address" column="AddressKey" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.