A many-to-one relation links one table to another. In this example, each student entry links to the student's house. The database schema might look like:
The House bean has two fields, id and name,
to model the house columns. House annotates
@Id to mark it as a primary key. It annotates
@Basic to mark it as a data column.
The Student bean adds a many-to-one field, house to the
id and name columns. It marks the
@ManyToOne to mark it as a many-to-one column.
@ManyToOne marks the
field as a many-to-one field. The
can also specify the target bean. By default, the field's type determines
the target bean.
@JoinColumn specifies the SQL column name and the target column for a many-to-one field.
The default column name is the name of the many-to-one field.
The default target column is the primary key of the target table.
The many-to-one relation provides two capabilities to the client:
The first relation capability extends SQL with relation paths
using the '.' operator like Java's field reference.
s is a student,
s.house is the student's house
s.house.name is the name of
the student's house. Resin translates the extended SQL to plain
The following example uses the extended SQL to return all students in a given house.
The second relation capability returns a live, persistent bean through
getHouse() method. Amber will perform any necessary
The example queries all students and prints their names and house names.