| ||||||||||||||||||||||||||||||||||
Find this tutorial in: /usr/local/resin/webapps/resin-doc/amber/tutorial/cmp-one2many
Try the Tutorial The @OneToMany relation adds collection extensions to the query language and provides a Java Collection containing the children. @OneToMany represents a collection of children belonging to a parent, like students in Gryffindor house at Hogwarts school. The Many-To-One tutorial illustrated that a many-to-one relation links one source entity to another target entity. A one-to-many relation links the target entity back to the source entity. In this example, each House has many Students, each Student has one House. House has a one-to-many relationship with Student, Student has a many-to-one relationship with House
Database SchemaThe database schema is unchanged from the Many-To-One tutorial , and might look like:
@OneToMany - the relation's "inverse" sideThe bean has a students field that returns the students that belong to the house. House annotates getStudents with @OneToMany to describe the relationship. The @JoinColumn is used to specify the foreign key, the field in that is the @ManyToOne link.
@ManyToOne - the relation's "owning" sideA @OneToMany always requires a corresponding @ManyToOne on the target entity. The Student has a house field annotated a @ManyToOne, unchanged from the Many-To-One tutorial .
The one-to-many relation provides a House the ability to get all of its Students. Resin will perform any necessary database lookup. The example queries all houses and prints their names and all of their students.
Result Collections: SELECT h.students
Joins: FROM House h, IN(h.students) s
Membership: s MEMBER OF h.students
Empty: h.students IS EMPTY
|