Collections: The @OneToMany Relation
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Amber
Lifecycle
@Table
Tutorials

CMP Field
CMP Basic
CMP Create
Transactions
CMP Query
CMP Many-to-One
CMP One-to-Many
CMP Many-to-Many
CMP Inherit
Sessions
CMP Many-to-One
Tutorials
CMP Many-to-Many

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

  1. Files in this tutorial
  2. Data Model: Object and Database Models
    1. Database Schema
    2. @OneToMany - the relation's "inverse" side
    3. @ManyToOne - the relation's "owning" side
  3. java.util.Collection: Using House.getStudents()
  4. Query extensions
    1. Result Collections: SELECT h.students
    2. Joins: FROM House h, IN(h.students) s
    3. Membership: s MEMBER OF h.students
    4. Empty: h.students IS EMPTY

Files in this tutorial

WEB-INF/resin-web.xml web.xml configuration
WEB-INF/classes/META-INF/persistence.xml persistence.xml configuration
WEB-INF/classes/example/Student.java The student bean
WEB-INF/classes/example/House.java The house bean
WEB-INF/classes/example/OneToManyServlet.java The 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:

SQL
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)
)

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

House.java
See it in: WEB-INF/classes/example/House.java
  @OneToMany (mappedBy="house")
  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 .

Student.java
See it in: WEB-INF/classes/example/Student.java

  @ManyToOne 
  @JoinColumn (name="house")
  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()
See it in: WEB-INF/classes/example/OneToManyServlet.java

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

Query extensions

Result Collections: SELECT h.students

h.students
SELECT h.students FROM House h WHERE h.name='Gryffindor'

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

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

Membership: s MEMBER OF h.students

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

Empty: h.students IS EMPTY

IS EMPTY
SELECT h FROM House h WHERE h.students IS EMPTY

Try the Tutorial


CMP Many-to-One
Tutorials
CMP Many-to-Many
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.