Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate 3.1.3: many-to-one mapping with property-ref to formula

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I also posted this in the Hibernate forums ( http://forum.hibernate.org/viewtopic.php?t=964216 ) but didn't get any response. So here is my problem - an hints would be highly appreciated:

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 )


<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.


[ September 01, 2006: Message edited by: Ilja Preuss ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Couple of ideas.

1. remove the column-name tag above.
2. Put the fomula inside the biotop where it adds 8000 to its ID, instead.
3. Maybe make it bi-directional.

It really should work.

Mark
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice food for thought.
See my reply in the Hibernate forum and don't forget ot vote, if it had helped you.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Spritzler:
1. remove the column-name tag above.


Then how do I tell Hibernate where to store the district number in the habitat table?


2. Put the fomula inside the biotop where it adds 8000 to its ID, instead.


That actually was my very first try (sorry for not telling about it). It works like a charm for querying, but once I try to *update/insert*, hibernate doesn't know what to do with the district number, naturally.


3. Maybe make it bi-directional.


I think I don't get the idea here. Could you please elaborate a little bit? Thanks!


It really should work.


I had hoped so.

The only way we got it to work is by moving the logic into a database view. I'd really like to get rid of that view, though.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the first suggestion, the property-ref defines the column, so you wouldn't need that in the relationship mapping. I didn't see a column tag in my slideset on ADV Hibernate.

I would say that definitely saving would be an issue, since I think that Hibernate thinks fomula fields are transient and not save in the database.

What if you implement the "equation" within the getter method of the field.

SO for instance you would add 8000 in the getter method and in the setter method subtract 8000 and see how it saves the data and queries.

I am just really thowing out random ideas that pop in my head.

Mark
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Spritzler:
For the first suggestion, the property-ref defines the column, so you wouldn't need that in the relationship mapping.


The property defines only the district side of the mapping, as far as I can see. Hibernate also needs to know about the habitat side, so that it knows which district to map to which habitat.


I would say that definitely saving would be an issue, since I think that Hibernate thinks fomula fields are transient and not save in the database.


Yes. A formula basically is something that gets put as-is into the select statement, if I understand correctly. Of course that couldn't possibly work for inserts or updates.


What if you implement the "equation" within the getter method of the field.


I'd think the equation needs already to be reflected in the SQL statement.


I am just really thowing out random ideas that pop in my head.


Which is very much appreciated!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The property defines only the district side of the mapping, as far as I can see. Hibernate also needs to know about the habitat side, so that it knows which district to map to which habitat.


That is kind of why I had said to have the relationship bi-directional. Currently it shows as one direction, if it is bi-directional then it should just know the property-ref and use the column that is defined in there.

Mark
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Spritzler:

That is kind of why I had said to have the relationship bi-directional. Currently it shows as one direction, if it is bi-directional then it should just know the property-ref and use the column that is defined in there.


But wouldn't it then also try to find that column on the district *table* instead of the habitat table?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really.

You have this

Parent_id and child_id. The only difference between the two are that one id is 8000 off, other than that then I consider this a PK to FK relationship, it is just that the PK of the child is acting as both. They can still be used to create the relationship bi-directional. Is this a correct assumption?

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic