Java 1.6
Hibernate 3.5.0-FINAL
Mysql 5.5
I'm convinced I'm doing something wrong here but I just can't figure out what it is. I've tried using a List and Set member variable for this relationship and both give me the same result, which is a null object. The join table is loaded with proper data and when I turn up Hibernate logging and find the sql that's executed (see below), I can paste this sql into my DBVis application and I DO get back the result I'm expecting. Here's what I have configured:
Consumer.java (my entity with the @ManyToMany annotation)
Code:
@Entity
@Table(name = "CONSUMER")
@XmlRootElement(name = "consumer")
@XmlAccessorType(XmlAccessType.NONE)
public class Consumer implements Persistable<Long>, Serializable {
...
@ManyToMany
@JoinTable(name = "CONSUMER_TAG",
joinColumns = @JoinColumn(name = "CONSUMER_UUID", referencedColumnName = "PRIM_UUID"),
inverseJoinColumns = @JoinColumn(name = "TAG_ID", referencedColumnName = "TAG_ID")
)
@XmlElement
private List<Tag> tags;
}
Tag.java
Code:
@Entity
@Table(name = "TAG")
@XmlRootElement(name = "tag")
@XmlAccessorType(XmlAccessType.NONE)
public class Tag implements Persistable<Long>, Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "TAG_ID")
private Long id;
@Column(name = "NAME")
private String name;
public Tag() {
}
@Override
public Long getId() {
return id;
}
@Override
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
CONSUMER_TAG table
Code:
CREATE TABLE IF NOT EXISTS consumer_tag (
consumer_uuid varchar(255) NOT NULL,
tag_id int(11) NOT NULL,
INDEX consumer_uuid_idx (consumer_uuid),
INDEX tag_id_idx (tag_id),
FOREIGN KEY (consumer_uuid) REFERENCES consumer(prim_uuid),
FOREIGN KEY (tag_id) REFERENCES tag(tag_id)
);
The SQL that Hibernate generates for this ManyToMany annotation is:
Code:
select
tags0_.CONSUMER_UUID as CONSUMER1_5_1_,
tags0_.TAG_ID as TAG2_1_,
tag1_.TAG_ID as TAG1_20_0_,
tag1_.NAME as NAME20_0_
from
CONSUMER_TAG tags0_
inner join
TAG tag1_ on tags0_.TAG_ID=tag1_.TAG_ID
where
tags0_.CONSUMER_UUID=?
If I substitute a real value for the CONSUMER_UUID constraint and run this in DBVis, I _do_ get back a single result. But when I call a consumer.getTags() on my Consumer bean, the result is null.
Does anyone see something I'm doing awfully wrong?
Thanks so much for any help or suggestions!
- Billy -