<document>
<deftable>
web.xml configuration
persistence.xml configuration
The student bean
The house bean
The test servlet
</deftable>

The database schema is unchanged from the Many-To-One tutorial, and might look like:

CREATE TABLE house ( id BIGINT PRIMARY KEY auto_increment, name VARCHAR(250), ) CREATE TABLE student ( id BIGINT PRIMARY KEY auto_increment, name VARCHAR(250), house BIGINT REFERENCES house(id) )

The House 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 Student that is the `@ManyToOne` link.

@OneToMany(mappedBy="house") private Set<Student> _students;

A @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.

@ManyToOne @JoinColumn(name="house") private House _house;

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.

private void doService(PrintWriter out) throws java.io.IOException { public void service(HttpServletRequest req, HttpServletResponse res) throws java.io.IOException, ServletException { PrintWriter out = res.getWriter(); res.setContentType("text/html"); String sql = "SELECT h FROM House h"; Query allHouse = _entityManager.createQuery("SELECT o FROM House o"); List houses = allHouse.getResultList(); for (int i = 0; i < houses.size(); i++) { House house = (House) houses.get(i); out.println("<h3>" + house.getName() + "</h3>"); for ( Student student : house.getStudents() ) { out.println( student.getName() + "<br>" ); } } } } SELECT h.students FROM House h WHERE h.name='Gryffindor' SELECT s FROM House h, IN(h.students) s WHERE h.name='Gryffindor' SELECT s FROM Student s, House h WHERE s MEMBER OF h.students SELECT h FROM House h WHERE h.students IS EMPTY
</document>