0

I can't really articulate what I want in the title very well but essentially is this possible?

public class Employee {
    public Address address;
...

public class address {
...

Very simple object model, an Employee that has an Address object property. In NHibernate when this is mapped will produce something like:

table Employee
    EmployeeId
    AddressId
...

table Address
    AddressId
...

So this is all good so far, my Employee table has a foreign key column to the Address table, perfect. What I want though is when I do a get with NHibernate that it doesn't join on that table and populate the Address object but instead instantiate the Address object and only populate the AddressId property.

Now before I get loads of responses about NHibernates lazy loading I already know. This is more of a "Is is possible" not a "is it a good idea" because I'm sure it's probably not. I just like to see how flexible NHibernate is.

David
  • 1,731
  • 24
  • 37

3 Answers3

4

One way would be to expose AddressId as a fully mapped item in the Employee class. This way you can get the AddressId from:-

Employee.AddressId

However said, nothing is loaded or extra select's are sent to the database if you just get the ID from:-

var addressId = Employee.Address.Id
Rippo
  • 22,117
  • 14
  • 78
  • 117
  • This looks to be the only solution yes, I just wish there was a way to stop NHibernate from pulling back the entire object other than through lazy loading. – David Feb 27 '12 at 17:17
  • 2
    This doesn't trigger a select statement to the database `var addressId = Employee.Address.Id` – Rippo Feb 27 '12 at 17:24
1

This doesn't make all that much sense to me, since you already have the AddressId as a reference to the Adress, so instantiating the object just to give you that, seem unnecessary. BUT, maybe you want something like this: http://ayende.com/blog/4378/nhibernate-new-feature-no-proxy-associations

edit, after comments: also, things like address can be mapped as components, maybe this will help with your requirements. see this link and google to get started: http://ayende.com/blog/3937/nhibernate-mapping-component

mindandmedia
  • 6,800
  • 1
  • 24
  • 33
  • I suppose I am not overly keen on NHibernates "load everything" or "lazy load but you have to do so within the session". I want the ability to create a new Employee and then an Address for that Employee and for NHibernate to save it to the db, but I dont want to automatically retrieve the Address object OR lazy load it on the fly. – David Feb 27 '12 at 12:26
0

Perhaps I'm misunderstanding the question but what you're asking for is how NHibernate lazy loading works by default. If you get an Employee instance, the address will be a proxy of Address with the AddressId set. Accessing any property besides AddressId will cause NHibernate to fetch the address record.

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • No I know how NHibernates lazy loading works I am curious as to whether this half measure can be achieved. I don't want to lazy load the address object nor do I want to retrieve it by default. All I want back is the AddressId but mapped to the proper object. – David Feb 27 '12 at 13:01
  • 1
    How is that any different from lazy loading? I'll second mindandmedia's response that what you're asking for makes no sense. – Jamie Ide Feb 27 '12 at 13:20
  • It's different because NHibernate needs you to declare all your properties as virtual and then creates a proxy that goes away and tries to fetch the data if accessed which throws an error if the session is closed. All I want is the ID back but maintain the object model. – David Feb 27 '12 at 13:41
  • @David, When you access the Id in that object it should not trigger the lazy load. So you should just have the id without lazy loading anything. This is just reiterating what Rippo says above – Cole W Feb 27 '12 at 19:38