-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Problem with many-to-one mapping: property-ref to formula
PostPosted: Thu Aug 31, 2006 10:56 am 
Newbie

Joined: Thu Aug 31, 2006 10:07 am
Posts: 2
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]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 02, 2006 10:51 am 
Beginner
Beginner

Joined: Wed Jan 25, 2006 10:16 am
Posts: 44
Location: Bangalore
I wonder the business use case behind this. I would use a simple HQL query for it.

Consider this example.
It works fine for me in MySQL and Hibernate 3.1

Class B analogous to your habitats and Class C analogous to your districts.


Code:
    <class name="B" table="b">
        <id name="b" column="b" type="java.lang.Long">
            <generator class="native"/>
        </id>
       
        <many-to-one name="refC" class="C"  column="ref_c" formula="ref_c + 10" />
   </class>

    <class name="C" table="c" >
        <id name="c" column="c" type="java.lang.Long">
            <generator class="native"/>
        </id>
    </class>


See the Class files.

Class C

private java.lang.Long c; //with setter and getter


Class B

private java.lang.Long b; //with setter and getter

private C refC; //with setter and getter
[/code]

_________________
Please vote if my Postings helps. :-)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 03, 2006 2:47 am 
Newbie

Joined: Thu Aug 31, 2006 10:07 am
Posts: 2
spatik wrote:
I wonder the business use case behind this.


It's really just an inconsistent database design. The district table could hold districts for the whole country. The habitat table only holds habitats for one federal state, for which the district number always begins with an eight, so they omitted that digit in the habitats table.

Quote:
I would use a simple HQL query for it.


The example below isn't using a HQL query, is it?

Quote:
Code:
    <class name="B" table="b">
        <id name="b" column="b" type="java.lang.Long">
            <generator class="native"/>
        </id>
       
        <many-to-one name="refC" class="C"  column="ref_c" formula="ref_c + 10" />
   </class>

    <class name="C" table="c" >
        <id name="c" column="c" type="java.lang.Long">
            <generator class="native"/>
        </id>
    </class>



It never occured to me that I could mix the column and formula attributes. I tried mixing column and formula *tags*, but that seemed to imply the semantics of a composite id.

What are the exact semantics of mixing column and formula attributes? And where can I learn more about it? Neither the online documentation, nor the books I use seem to say anything about these cases.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 04, 2006 4:20 am 
Beginner
Beginner

Joined: Wed Jan 25, 2006 10:16 am
Posts: 44
Location: Bangalore
Quote:
The example below isn't using a HQL query, is it?

Nope.

Quote:
What are the exact semantics of mixing column and formula attributes? And where can I learn more about it? Neither the online documentation, nor the books I use seem to say anything about these cases.


I use eclipse to write my XML with the DTD in place. So I use the aut-complete feature to list me the options that are available. But that doesn' t make you learn exactly. There are lots of online examples which have typical business cases and how they are tackled using HBM.

Moreover using these tags and attributes becomes easier the more you work with them. You would probably know the exact tags and attributes to use as and when you are familiarised with them.
Thinking retrospectively, I was competely dumb on using these. Later as and when I started using them, you learn how they are designed and sometimes you can even use certain tags to cheat the HBM to map to an underlying E-R in database which is not in sync with the O-R in java world (That again drives the inconsistent E-R design we have, but sometimes changing something which is existing might impact business and involve lots of time to change the existing model and schema).

Fine end of story. Take away, three things,

1. Try learning lots of examples.
2. Use tags and attributes and play around with it.
3. Try Using an XML document builder.

_________________
Please vote if my Postings helps. :-)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 04, 2006 4:20 am 
Beginner
Beginner

Joined: Wed Jan 25, 2006 10:16 am
Posts: 44
Location: Bangalore
Quote:
The example below isn't using a HQL query, is it?

Nope.

Quote:
What are the exact semantics of mixing column and formula attributes? And where can I learn more about it? Neither the online documentation, nor the books I use seem to say anything about these cases.


I use eclipse to write my XML with the DTD in place. So I use the aut-complete feature to list me the options that are available. But that doesn' t make you learn exactly. There are lots of online examples which have typical business cases and how they are tackled using HBM.

Moreover using these tags and attributes becomes easier the more you work with them. You would probably know the exact tags and attributes to use as and when you are familiarised with them.
Thinking retrospectively, I was competely dumb on using these. Later as and when I started using them, you learn how they are designed and sometimes you can even use certain tags to cheat the HBM to map to an underlying E-R in database which is not in sync with the O-R in java world (That again drives the inconsistent E-R design we have, but sometimes changing something which is existing might impact business and involve lots of time to change the existing model and schema).

Fine end of story. Take away, three things,

1. Try learning lots of examples.
2. Use tags and attributes and play around with it.
3. Try Using an XML document builder.

_________________
Please vote if my Postings helps. :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.