Reputation: 5024
I have 2 tables:
create table EMPLOYEE (
id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name VARCHAR(20) default NULL,
salary INT default NULL,
address INT NOT NULL,
PRIMARY KEY (id)
);
create table ADDRESS (
id INT NOT NULL auto_increment,
street_name VARCHAR(40) default NULL,
city_name VARCHAR(40) default NULL,
state_name VARCHAR(40) default NULL,
zipcode VARCHAR(10) default NULL,
PRIMARY KEY (id)
);
where EMPLOYEE.address
is a foreign key mapped to ADDRESS
My mapping file is defined as follows:
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
<many-to-one name="address" column="address" unique="true"
class="Address" not-null="true"/>
</class>
<class name="Address" table="ADDRESS">
<meta attribute="class-description">
This class contains the address detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="street" column="street_name" type="string"/>
<property name="city" column="city_name" type="string"/>
<property name="state" column="state_name" type="string"/>
<property name="zipcode" column="zipcode" type="string"/>
</class>
</hibernate-mapping>
But, when I pull Employee
objects all the normal fields are populated normally except the Address
field in the Employee
plain old object has all the values set to null. What could be causing this?
Upvotes: 1
Views: 64
Reputation: 2403
Unfortunately a bit long for a comment, but hopefully this will give you something to look at:
It is my understanding that NHibernate
is actually "lazy by default", this means that the Address
object won't be populated until requested, and then (if I remember correctly) only if you are still connected. I've had issues in the past of passing partially loaded objects to a view
in my MVC
projects and yours sounds similar.
That being said, I've read on several articles that you NHibernate
doesn't lazy-load for many-to-one
relationships, however I still think this is probably worth looking into. The many-to-one
relationship requires the use of a foreign key, and therefore the referencing column must be the primary key. I.e. EMPLOYEE.address = ADDRESS.id
.
Once you solve your problem please post what was actually wrong as I am looking to improve my knowledge in NHibernate
.
You might find the following articles useful:
And these Questions:
Upvotes: 1