Hi all, I have an issue with my current mappings and would to know if it's possible to create entities that will model my required relationship so that Hibernate will create my schema when I fire up my application.
The relationship I want looks like this:
The thing is that the Join table can actually contain rows that don't link to any Elements. The structure represents categorising of elements based on the "type" and "value" pair and are entered in to the system outside of this particular application.
I know that this isn't great in terms of database design, but the problem i have is that I can't make any changes to the existing tables. I'm adding in ElementCategory and the join but can't do anything that will alter the layout in Element.
What I would like to be able to do is set my Element Hibernate Entity to contain a list of Categories, via a mapping, so that I can actually see what Categories my element belongs to AND so that hibernate creates the table for me.
Here's what I've got so far:
Code:
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "ELEMENT_ELEMENTCATOGORY", joinColumns = {
@JoinColumn(name = "type", referencedColumnName = "type"),
@JoinColumn(name = "value", referencedColumnName = "value") }, inverseJoinColumns = {
@JoinColumn(name = "id", referencedColumnName = "id") }
)
@Column(name = "category")
public List<ElementCategory> getCategories() {
return categories;
}
This does most of what I want, it creates my tables as above, exactly how I want them bar one thing, there's a Unique Constraint added in to the Element Table on the (type,value) pair. I don't want this because multiple elements can have the same type and value pair, I need to be able to stop the Unique Constraint from begin created, but can't figure out how with the current mapping, can I do this? Am I missing the point of a Many to Many relationship?