Hibernate version: 3.1.3
Hi,
we have two tables in our database, modeling habitats and districts. There is a many-to-one relationship from habitats to districts. We have a fixed set of districts, that is the district table is read-only as far as our system is concerned.
Now the problem is that the "foreign key" in the habitats table isn't a true foreign key - the relationship is of the form
foreign-key-value-in-habitats = primary-key-value-in-districts - 8000
That is, when a habitat row has a district number 215, the primary key of the actual district in the district table is 8215.
In the object model, we have a District class and a Habitat class, and I like a Habitat object to have a reference to a District object.
I tried to solve this by using a formula in the mapping (stripped down to the relevant parts):
(short lesson in German: Biotop == Habitat, Kreis == district ;) )
Code:
<class name="Biotop" table="NAIS_BIOTOP">
...
<many-to-one class="Kreis" name="kreis" property-ref="nummerInBw" cascade="none">
<column name="KREIS_NR"/>
</many-to-one>
</class>
<class name="Kreis" table="UIS_KREIS">
<id name="nummer">
<column name="KREIS_NR"/>
</id>
<property name="nummerInBw">
<formula>KREIS_NR - 8000</formula>
</property>
</class>
Again, remember that the Kreis table is read only - we never need to write into it. We *need* to write the KREIS_NR column in the Biotop, though.
I think *logically* it should be possible to do this, but it seems that Hibernate doesn't support formulas in properties that are referenced from many-to-one associations - at runtime, I get ClassCastException posted below.
Am I missing something? Is this just not (yet?) supported by Hibernate? Is it a bug?
Any hint would be highly appreciated.
java.lang.ClassCastException: org.hibernate.mapping.Formula
at org.hibernate.mapping.ManyToOne.createPropertyRefConstraints(ManyToOne.java:62)
at org.hibernate.cfg.HbmBinder$ManyToOneSecondPass.doSecondPass(HbmBinder.java:2673)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1012)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1168)[/code]