Hi,
it seems that I try something that anyone tried before. At least Google shows that there were nobody trying this ;)
I have an entity with three many-to-may-relationships. The join tables only contain the ids of the two joined tables.
Code:
COMPONENT {
COMPONENT_ID(number),
...
}
LANGUAGE {
LANGUAGE_ID(number),
...
}
PLATFORM {
PLATFORM_ID(number),
...
}
T_RELEASE {
RELEASE_ID(number),
...
}
T_RELEASE_COMPONENT {
T_RELEASE_RELEASE_ID(number),
COMPONENT_ID_COMPONENT_ID(number)
}
RELEASE_LANGUAGE {
T_RELEASE_RELEASE_ID(number),
LANGUAGE_ID_LANGUAGE_ID(number)
}
RELEASE_PLATFORM {
T_RELEASE_RELEASE_ID(number),
PLATFORM_ID_PLATFORM_ID(number)
}
I use Hibernate Annotations, so I tried to establish those relationships this way:
Code:
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="T_RELEASE_COMPONENT",
joinColumns=@JoinColumn(name="T_RELEASE_RELEASE_ID"),
inverseJoinColumns=@JoinColumn(name="COMPONENT_ID_COMPONENT_ID")
)
public List<Component> getComponents() {
return components;
}
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="T_RELEASE_LANGUAGE",
joinColumns=@JoinColumn(name="T_RELEASE_RELEASE_ID"),
inverseJoinColumns=@JoinColumn(name="LANGUAGE_ID_LANGUAGE_ID")
)
public List<Language> getLanguages() {
return languages;
}
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="T_RELEASE_PLATFORM",
joinColumns=@JoinColumn(name="T_RELEASE_RELEASE_ID"),
inverseJoinColumns=@JoinColumn(name="PLATFORM_ID_PLATFORM_ID")
)
public List<Platform> getPlatforms() {
return platforms;
}
Hibernate complained that it can't fetch multiple bags. I asked Google for help and found
Eyal Lupus blog post on this. Since I don't want to use lazy fetching or introduce an index column I tried to use @CollectionId in order to switch from bag semantics to idbag.
So my class turned to:
Code:
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="T_RELEASE_COMPONENT",
joinColumns=@JoinColumn(name="T_RELEASE_RELEASE_ID"),
inverseJoinColumns=@JoinColumn(name="COMPONENT_ID_COMPONENT_ID")
)
@CollectionId(generator = "identity", type = @Type(type = "number"),
columns = {
@Column(name = "T_RELEASE_RELEASE_ID"),
@Column(name = "COMPONENT_ID_COMPONENT_ID")
})
public List<Component> getComponents() {
return components;
}
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="T_RELEASE_LANGUAGE",
joinColumns=@JoinColumn(name="T_RELEASE_RELEASE_ID"),
inverseJoinColumns=@JoinColumn(name="LANGUAGE_ID_LANGUAGE_ID")
)
@CollectionId(generator = "identity", type = @Type(type = "number"),
columns = {
@Column(name = "T_RELEASE_RELEASE_ID"),
@Column(name = "LANGUAGE_ID_LANGUAGE_ID")
})
public List<Language> getLanguages() {
return languages;
}
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="T_RELEASE_PLATFORM",
joinColumns=@JoinColumn(name="T_RELEASE_RELEASE_ID"),
inverseJoinColumns=@JoinColumn(name="PLATFORM_ID_PLATFORM_ID")
)
@CollectionId(generator = "identity", type = @Type(type = "number"),
columns = {
@Column(name = "T_RELEASE_RELEASE_ID"),
@Column(name = "PLATFORM_ID_PLATFORM_ID")
})
public List<Platform> getPlatforms() {
return platforms;
}
I thought the columns attribute of @ColumnId specifies the primary key of the join table. And since the primary key is a composite of both columns I included them both. But deploying this results in a complaint of Hibernate that "T_RELEASE_RELEASE_ID" is used multiple times. Sure it's used multiple times, but on different tables?!?
Since I use JSF for representation I really would like to use java.util.List and not a Set or whatever. And since this table structure is fixed I don't want to introduce an additional column. Is there any way to solve this?
Thanks in advance
Newlukai