I am migrating a data access layer to hibernate, a few classes at a time. I have had
Partition,
Meter and
StoreMeter working fine for a while, and am now adding
MeterChannel. Meter joins one-to-one with StoreMeter and many-to-one with Partition. MeterChannel joins many-to-one with Meter. So when I load a MeterChannel, I am non-lazily, and with fetch="join", loading Meter and Partition. I am lazily loading StoreMeter and that eagerly loads its own Partition object.
Most of the time I load objects via queries, and this is all working fine. In one case I was loading MeterChannels via load (and I've tested it with get, same results) and the last join in the sequence, from Meter to Partition, isn't being fully loaded before the postLoadEventListener is being invoked. I have code in there which checks the data in the Partition object and this is failing. The Partition object has its id loaded but no other values.
Are there any differences between session.get()/session.load() and query.list() in the loading of objects via a chain of many-to-one relationships like this? I'm happed to change my code to use a query which does the same thing as load(), but I'd like to know why this is happening.
I would imagine that there's something easily-explained at work here, but just in case, here's the important bits from the mapping file. The elipses denote extra, unimportant properties that I've removed for brevity.
Code:
<class name="PartitionImpl" table="manage.dbo.vwPartition"
mutable="false" lazy="false">
...
</class>
<class name="StoreMeterImpl" table="Store.dbo.Meter" mutable="true" lazy="false">
...
<many-to-one name="Partition"
class="com.tml.framework.manage.impl.PartitionImpl"
insert="false" update="false" unique="true" not-null="true" lazy="false">
</class>
<class name="MeterImpl" table="Meter" mutable="true">
<many-to-one name="StoreMeter" class="StoreMeterImpl" column="StoreMeterID"
unique="true" fetch="select" lazy="true" cascade="all"/>
...
<many-to-one name="Partition" column="ManagementCompanyID"
class="com.tml.framework.manage.impl.PartitionImpl"
fetch="join" insert="true" update="false" not-null="true" lazy="false">
<formula>
(select Meter.ManagementCompanyID from Meter
where Meter.StoreMeterID = MeterID)
</formula>
</many-to-one>
</class>
<class name="MeterChannelImpl" table="MeterChannel" lazy="false">
...
<many-to-one name="Meter" column="MeterID" class="MeterImpl"
fetch="join" not-null="true" update="false" unique="true" lazy="false"/>
</class>