Question

Entities

We have an entity called Product which is loaded using NHibernate.

Product has a category which NHibernate happily populates for me.

Database

In the database, Product has a foreign key for category.

Scenario

User edits this Product (via a web interface) and chooses a different category (say instead of "Fish" we select "Veg").

This is probably a dropdown list, with each category shown. When they choose a different category we get an int key.

Problem

Obviously we now want to save the changes to Product but in effect the only change is to save a new int (say 2, instead of 1).

So we retrieve the existing Product, and now comes the problem.

We don't have a "CategoryID" field on Product, we only have a Category property.

But we don't really want to retrieve the category (by id) just to assign it to the Product.

So I guess what I want to know is should we...

a) Add a CategoryID property to Product

b) Create a new category, assign it the relevant id and attach that to Product (but surely that will cause errors, or overwrite the existing category)

c) Retrieve (lookup) the category from the system (by id) and attach that to the Product

d) Do something else entirely!

Was it helpful?

Solution

Updated: Session.Load is the correct answer

product.Category = session.Load<Category>(2);

session.Save(product);

OTHER TIPS

It looks like you might be able to using the Session.Load(id) functionality.

Session.Load is a special method that returns a proxy with the ID until you request another property at which point it loads. It throws an error if there is no item matching the ID. Try something like:

product.Category = Session.Load<Category>(2); //2 being the new category ID

Session.SaveOrUpdate(product);

I just did a little testing and it did not seem to pull back the entire Category.

Use NH's EnumStringType<T> to map your Category as an enum to the respective database value (which can be a string or a number). You'll find quite a few usage examples, if you google for it.

HTH!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top