I have the Category where each category can have multiple childs (downlevel categories) and multiple parents (uplevel categories).
I mapped this is the following way:
@Entity
public class Category {
@Id
public String Url;
@OneToMany
private Set<Category> childs = new HashSet<Category>();
@OneToMany
private Set<Category> parents = new HashSet<Category>();
@OneToMany(cascade=CascadeType.ALL)
private Set<Person> persons = new HashSet<Person>();
public String Title;
public boolean Done;
I found that Hibernate coded this entity with the following table
CREATE TABLE
category_category
(
Category_Url VARCHAR(255) NOT NULL,
parents_Url VARCHAR(255) NOT NULL,
childs_Url VARCHAR(255) NOT NULL,
PRIMARY KEY (Category_Url, childs_Url),
CONSTRAINT FK8635931F3275D6D7 FOREIGN KEY (Category_Url) REFERENCES category (Url) ,
CONSTRAINT FK8635931F569C2962 FOREIGN KEY (parents_Url) REFERENCES category (Url) ,
CONSTRAINT FK8635931F6ADF3430 FOREIGN KEY (childs_Url) REFERENCES category (Url),
CONSTRAINT parents_Url UNIQUE (parents_Url),
CONSTRAINT childs_Url UNIQUE (childs_Url),
which is absolutely wrong because childs and parents of some row are not related to each other and should not contained in one tuple.
I suppose Hibernate would create two tables category_category_1(Category_Url,childs_Url) and category_category_2(Category_Url,parents_Url) with on relating from this to childs and another — from this to parents.
Why Hibernate did as it did and how to make it did correctly?
You can give distinct names to link tables using
@JoinTable: