0
votes

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.

3

3 Answers

4
votes

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
1
votes

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

0
votes

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.