Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubts mapping several relations

 
David Crecente
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am having problems mapping two relations among tables.
I am using EJB3, so standard java persistence.


1) First relation definition.
The idea here is that a category may belongs to many categories. A many
to many relation but with itself.




2) Second relation definition.
It is a simple many to many relation between CATEGORY and LOCALE but
the table CATEGORY_LOCALE has more properties. Mayby I might write a
CategoryLocaleEntity with a composite primary key but this composite
key should be a composition of id in CATEGORY and id in LOCALE.




Thank you in advance.
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

You didn't specify the Java classes. That is where the mappings take place. Assuming your relationship is bidirectional then your category class would look something like:

@Entity
public class Category {
@Id int id;
String description;

// Other properties...

// Collection of children
@ManyToMany
@JoinTable(name="PARENT_CATEGORY",
joinColumns=@JoinColumn(name="PARENT_ID"),
inverseJoinColumns=@JoinColumn(name="CATEGORY_ID"))
Set<Category> childCategories;

// Collection of parents
@ManyToMany(mappedBy="childCategories")
Set<Category> parentCategories;
}

For your CATEGORY-LOCALE relationship then since you have additional properties in the relationship table you would need to do as you suggested and have an intermediate entity to map to the CATEGORY_LOCALE table, and then relate that entity to each of the Category and Locale objects.
 
David Crecente
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,
thank you for your answer.
I didn't specify java classes because I tried serveral mappings and I didn't get correct constrains for my tables.

Finally I am creating the model in the database and reverse engineering later. This way is much quick.

Thank you again.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic