Caucho maker of Resin Server | Application Server (Java EE Certified) and Web Server


Resin Documentation

Aug 2012: Resin outscales C-based web server nginx in AutoBench benchmark
Feb 2012: NetCraft survey says Resin experiencing strong growth in last year and used in number of the Million Busiest Sites.
home company blog wiki docs 
app server web server 
health cloud java ee pro 
 Resin Server | Application Server (Java EE Certified) and Web Server

collections: the @onetomany relation

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


Files in this tutorial

WEB-INF/resin-web.xmlweb.xml configuration
WEB-INF/classes/META-INF/persistence.xmlpersistence.xml configuration
WEB-INF/classes/example/Student.javaThe student bean
WEB-INF/classes/example/House.javaThe house bean
WEB-INF/classes/example/OneToManyServlet.javaThe test servlet

Data Model: Object and Database Models

Database Schema

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

  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)

@OneToMany - the relation's "inverse" side

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.
  private Set<Student> _students;

@ManyToOne - the relation's "owning" side

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.
  private House _house;

java.util.Collection: Using House.getStudents()

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.

Using House.getStudents()
private void doService(PrintWriter out)
  public void service(HttpServletRequest req, HttpServletResponse res)
    throws, ServletException
    PrintWriter out = res.getWriter();


    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>" );

Query extensions

Result Collections: SELECT h.students

SELECT h.students FROM House h WHERE'Gryffindor'

Joins: FROM House h, IN(h.students) s

SELECT s FROM House h, IN(h.students) s WHERE'Gryffindor'

Membership: s MEMBER OF h.students

SELECT s FROM Student s, House h WHERE s MEMBER OF h.students

Empty: h.students IS EMPTY

SELECT h FROM House h WHERE h.students IS EMPTY


Copyright © 1998-2012 Caucho Technology, Inc. All rights reserved. Resin ® is a registered trademark. Quercustm, and Hessiantm are trademarks of Caucho Technology.