There are two entity classes:
@Entity
public class Person {
@ManyToOne(cascade = CascadeType.PERSIST)
private Hobby hobby;
}
@Entity
public class Hobby {
private String hobby;
}
The Person.hobby
field should take only values, that are found in the "hobby" table. So I want to define a set of Hobbies
, from which a Person
can choose exactly one. Of course, one Hobby can be shared by many Persons and the "hobby" table should not be altered when a new Person
with a Hobby
is added.
In fact this would realize an enumeration, which is not coded in Java, but defined in the underlying database "hobby" table. So if new Hobbies have to be added, only the database and not the codebase have to be changed.
How is this realized with hibernate (4.3) and JPA annotations?