Hello,
Until recently I have been using Hibernate version 3.2.0. Somehow early on, (still pretty new to Hibernate, was newer then), I got the impression that in order to create many-to-one relationship between tableA and tableB, where the column in tableA that referenced a column in tableB was also the primary key of tableA, that I would need to:
1. Create a property to sit alongside the id column in table a, with "insert="false" update="false" attributes, so that it would be available for key reference from tableB's many-to-one relationship.
For example, TableAPDO.hbm.xml would contain:
Code:
<id name="qualifierID" type="big_decimal">
<column name="QUALID" precision="38" scale="0" />
</id>
<property name="qualifierID" insert="false" update="false">
<column name="QUALID" />
</property>
2. Configure the relationship in tableB many-to-one, linking back to table A via a property-ref="name_of_property_in_table_a".
For example, TableBPDO.hbm.xml would contain:
Code:
<many-to-one name="qualifierDetail" class="TableAPDO" property-ref="qualifierID" insert="false" update="false" not-null="true" fetch="select" not-found="ignore">
<column name="QUALIFIER" />
</many-to-one>
This worked as expected.
When I tried to upgrade to version 3.2.4 of Hibernate, this produced a NullPointerException, deep in the Hibernate AbstractEntityParser where it was trying to link back to a unique column. I realize that the "right" way to do this type of relationship is to leave the property-ref off of the many-to-one relationship configured in table B, and just allow it to accept the default behavior of linking to the primary key column in tableA. My question is, should this not work? I noticed that this only DOESN'T work if the name attribute on the id column is THE SAME as the property name on the "insert="false" update="false" property added in tableA's .hbm.xml linking to the same column as the id. If the name is different at all, the relationship works, and the NullPointerException goes away. I have a simple project which reproduces this if it would be helpful to anyone in attempts to experiment with this.
Thoughts? Can I provide any more information?
The stacktrace was as follows:
Code:
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1645)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:608)
at org.hibernate.type.EntityType.resolve(EntityType.java:382)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3044)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:869)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:864)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.get(Unknown Source)
at TestHibernate.tryHibernateDBConnectionAndGet(TestHibernate.java:26)
at TestHibernate.main(TestHibernate.java:20)
Code:
Thanks,
Jeff