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.  [ 4 posts ] 
Author Message
 Post subject: composite-id, one-to-one mapping, no constraints in database
PostPosted: Tue Jan 20, 2009 6:44 am 
Newbie

Joined: Tue Jan 20, 2009 6:13 am
Posts: 2
Location: Netherlands
I'll be short. I'm using Hibernate 3. My problem is about one-to-one mapping with composite-id's (I think). None of the examples I have seen so far are the same as my situation.

Goal

Without any mapping in the database itself, being able to do:
Code:
Apple apple = // retreive apple from Hibernate
String juice = apple.getOrange().getJuice();


Problem

org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class fruit.Orange, got class fruit.Apple

Why does it not work? He should expect an Orange in the mapping, but why is there an Apple?

Database

I have two tables without any foreign key constraints. For APPLE the primary key is number and subnumber. For ORANGE the primary key is nr and subnr.

APPLE (number, subnumber, color)
ORANGE (nr, subnr, juice)

Mapping

Code:
<class name="fruit.Apple" table="APPLE">
  <composite-id>
    <key-property name="number" column="number" type="long" />
    <key-property name="subnumber" column="subnumber" type="int" />
  </composite-id>
  <one-to-one name="orange" class="fruit.Orange" />
</class>


Code:
<class name="fruit.Orange" table="ORANGE">
  <composite-id>
    <key-property name="number" column="nr" type="long" />
    <key-property name="subnumber" column="subnr" type="int" />
  </composite-id>
</class>


Java

Code:
public class Apple {
  private long number;
  private int subnumber;
  private Orange orange;
  // getters and setters
}


Code:
public class Orange {
  private long number;
  private int subnumber;
  // getters and setters
}


Top
 Profile  
 
 Post subject: you can't compare apples with oranges ;-)
PostPosted: Tue Jan 20, 2009 6:58 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
This happens, because you do not have an ID-Class in your mapping. In that case, objects itself are the identifier. No problem so far, but you have an OneToOne-association: these are normally mapped using the primary key also as the foreign key. So hibernate uses Apple's primary key (an apple) as foreign key to load orange (which wants an orange).

To avoid this, create an IdClass, which has the Id-Attributes as properties:
Code:
public class FruitId {
  private long number;
  private long subnumber;
  //Getters, Setters
}

Now in your mapping just add the class attribute to your composite-id:
Code:
<composite-id class="FruitId"> ...

That should solve this problem. See also the second approach here (and if you understand German look here for a topic relating the same issue).

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2009 10:56 am 
Newbie

Joined: Tue Jan 20, 2009 6:13 am
Posts: 2
Location: Netherlands
Thank you, mmerder. It turned out I was on the right track, but the Orange-view turned out to be empty! :S So it took me some more time than needed... But your post was helpful.


Top
 Profile  
 
 Post subject: Re: composite-id, one-to-one mapping, no constraints in database
PostPosted: Fri Jun 26, 2009 2:29 am 
Beginner
Beginner

Joined: Wed Aug 22, 2007 5:53 am
Posts: 38
I also have the same problem...
I tried with the approach of having a seperate class for Composite-Id.. I have one-to-one relationship between Product and Product-Id..

I have Product (PId, PDesc) as composite-key
I have ProductBasic (ProductId, ProductDesc) as composite-Key

Case 1: I created one Composite class which is common for both Product and ProductBasic..
class CompProductRelated{
string pid;
string pdesc;
}
then try to fetch Product.. It worked !!!

Case 2: If i create seperate Composite class for Product & ProductBasic viz. CompProduct and CompProductBasic respectively as :

class CompProduct{
string productId;
string productDesc;
}

and not if i define the mapping accordingly as:
In Product.hbm.xml
<composite-id name="compProduct" class="CompProduct">
<key-property name="pid" type="string" column="PRODUCTID" length="10"></key-property>
<key-property name="desc" type="string" column="DESC" length="10"></key-property>
</composite-id>

In ProductBasic.hbm.xml
<composite-id name="compProductBasic" class="CompProductBasic">
<key-property name="productId" column="PRODUCTID" type="string" length="10"/>
<key-property name="productDesc" column="DESC1" type="string" length="10"/>
</composite-id>
Now while fetching Product, it gives the following exception:
org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class model.CompProductBasic, got class model.CompProduct

NOTE: This is the similar exception if i don't use a seperate class for CompositeId..

I think this hibenate restriction (of having common CompositeId class for one-to-one relationship) is not good as it puts a constraint on how domain model (java) should be which could not be possible everytime in legacy applications..


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.