This is a Hibnerate polymorphism question and a data model design question; they are intertwingled. I\'ve used Hibernate in the past, and have enjoyed it, but sometimes I find
This is absolutely possible. You could associate the notes with the abstract Fruit
class instead of repeating them in each of the implementations:
@Entity
@Inheritance
public abstract class Fruit {
private Set notes;
...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "fruit")
public Set getNotes() {
return notes;
}
}
@Entity
public class Apple extends Fruit {
...
}
@Entity
public class Orange extends Fruit {
...
}
@Entity
public class Note {
private String theNote;
@ManyToOne
private Fruit fruit;
...
}
Ét voilà!
-- Addition based on comment: JPA provides multiple strategies for dealing with inheritance. The relevant section in the Java EE tutorial should help you get started.
Basically, your options are:
Another edit: Noticed this is a Hibernate, not a JPA question. Does not make too much of a difference, though, as the options are the same. Here's the relevant section in the Hibernate docs.