I'm just getting started building my JPA schema in a Play Framework web app. I have a reasonable understanding of SQL, but I'm a JPA newbie, and I'm being tripped up at the first hurdle.
From the Play tutorials I'm assuming you just create your Java classes and JPA/Play will automagically create the schema for you.
So I want to create a ManyToMany relationship between two model classes, Rankable and Tag:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Rankable extends Model {
public String name;
private Set<Tag> tags;
@ManyToMany()
@JoinTable(name = "RANKABLE_TAGS")
public Set<Tag> getTags() {
return tags;
}
@ManyToMany()
@JoinTable(name = "RANKABLE_TAGS")
public void setTags(final Set<Tag> tags) {
this.tags = tags;
}
}
And the other class:
@Entity
public class Tag extends Model {
public String name;
public String description;
private Set<Rankable> rankables;
@ManyToMany(mappedBy = "tags")
public Set<Rankable> getRankables() {
return rankables;
}
@ManyToMany(mappedBy = "tags")
public void setRankables(final Set<Rankable> r) {
rankables = r;
}
}
But I keep getting the following error:
A JPA error occurred (Unable to build EntityManagerFactory): Could not determine type for: java.util.Set, at table: Rankable, for columns: [org.hibernate.mapping.Column(tags)]
What am I doing wrong?
In our case, the reason was that we had some annotations on the field and some on the getters. In other words, for a specific field, the annotations should be either on the field or on the getter. Combining them on the getters solved the problem for us. Seems like the exception do not show the real cause of the problem. By the way, we used this syntax for the manytomany annotations:
Entity:
@ManyToMany
@JoinTable(name = "join_table", joinColumns = { @JoinColumn(name =
"leftjoinid") }, inverseJoinColumns = { @JoinColumn(name = "rightjoinid") })
Entity:
@ManyToMany
@JoinTable(name = "join_table", joinColumns = { @JoinColumn(name =
"rightjoinid") }, inverseJoinColumns = { @JoinColumn(name = "leftjoinid") })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With