I'm trying to implement a basic tagging system with hibernate. The problem is that everyone has been subclassing Tag to make the mapping easier. I want to keep my Tag object agnostic to how it's used. It should never be specific to a type. However, the mapping has proven to be very difficult. Here is what I am trying to accomplish.
I am starting out with two classes Tag and Prefix. Prefix will contain a collection of tags. I have 3 tables. prefix, tags and tagged where tagged is a table joining prefix and tags. The enumeration lists the other objects 开发者_运维技巧that I will tag. This is how I want to support different types.
It looks like this:
CREATE TABLE tagged(
tag_id INT(11) NOT NULL,
object_id INT(11) NOT NULL,
discriminator ENUM('PREFIX', 'ROOT', 'SUFFIX')
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
At the heart of my mapping I have a one-to-many mapping
@OneToMany
@JoinTable(name = "tagged", joinColumns = { @JoinColumn(name = "object_id") }, inverseJoinColumns = @JoinColumn(name = "id"))
public List<Tag> getTags() {
return tags;
}
So, the question is, how do I join to the discriminator table and set a value for it? I initially tried:
@OneToMany
@JoinTable(name = "tagged", joinColumns = @JoinColumn(name = "object_id"), inverseJoinColumns = @JoinColumn(name = "id"))
@Where(clause = "discriminator='PREFIX'")
public List<Tag> getTags() {
return tags;
}
This didn't work. Hibernate was looking in the tags table for the discriminator. Any ideas? Thanks!
Have you tried using the WhereJoinTable annotation? Given its documentation, it should do what you want.
精彩评论