-->
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.  [ 1 post ] 
Author Message
 Post subject: Composite key, with foreign keys
PostPosted: Mon Apr 10, 2006 8:39 pm 
Newbie

Joined: Wed Apr 05, 2006 4:33 pm
Posts: 5
I have not been able to come up with a mapping that works for the following scenario:
Code:
class Recipe
{
   List<IngredientQty>ingredients;
   int id;
}

class Ingredient
{
   int id;
}

class IngredientQty
{
   Recipe recipe;
   Ingredient ingredient;
   double qty;
}


I've tried numerous things but none of them have worked. My first reaction, after reading documentation was:

Code:
@Entity
@Table(name="recipe")
class Recipe implements Serializable
{
   @OneToMany(targetEntity = IngredientQty.class)
   @JoinColumn(name="recipe_id")
   List<IngredientQty>ingredients;
   @Id
   @Column(name="id")
   int id;
}

@Entity
@Table(name="ingredient")
class Ingredient implements Serializable
{
   @Id
   @Column(name="id")
   int id;
}

@IdClass(IngredientQtyPK.class)
@Entity
@Table(name="recipe_x_ingredient")
class IngredientQty implements Serializable
{
   @Id
   @ManyToOne
   @JoinColumn(name="recipe_id")
   Recipe recipe;
   @Id
   @ManyToOne
   @JoinColumn(name="ingredient_id")
   Ingredient ingredient;
   @Column(name="qty")
   double qty;
}

@Embeddable
class IngredientQtyPK implements Serializable
{
   Recipe recipe;
   Ingredient ingredient;
   //I also implement hashcode and equals,
}


I've also tried using the embeddedId solution, but again, with no success. Either I get an error from hibernate that it can't serialize integer columns to complex objects, or it tries to retrieve a column named "recipe" from the recipe_x_ingredient table.

I did manage to make the whole thing work if I change all of my complex objects to simple integers, but that is not very helpful, since I then need a global singleton to be able to get back to my objects (which not only is yucky but also limits other functionality I need)

P.S. I've simplified the code, hopefully haven't broken it too much on the way.

Please help!


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

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.