Many-to-Many CMP
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 One-to-Many
Tutorials
CMP Inherit

Find this tutorial in: /usr/local/resin/webapps/resin-doc/amber/tutorial/cmp-many2many
Try the Tutorial

Illustrates using many-to-many relations of EJB 3.0.

  1. Files in this tutorial
  2. Entity Beans
  3. @ManyToMany
  4. Client

Files in this tutorial

WEB-INF/resin-web.xml resin-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/Course.java The course bean
WEB-INF/classes/example/Grade.java The grade bean
WEB-INF/classes/example/ManyToManyServlet.java The course servlet

Entity Beans

The many-to-many relation connects two tables with an association table. In the example, each Student takes several Courses. A grade_map table connects the two. Using the many-to-many relation, the application can return the student's courses or the students in a course.

SQL Schema
CREATE TABLE Course (
  course_id BIGINT PRIMARY KEY,

  name VARCHAR(255)
)

CREATE TABLE Student (
  student_id BIGINT PRIMARY KEY,

  name VARCHAR(255)
)

CREATE TABLE grade_map (
  id BIGINT PRIMARY KEY auto_increment,

  student_id BIGINT REFERENCES Student(student_id),
  course_id BIGINT REFERENCES Course(course_id)
)

The Course has an @Id and a data column for the name.

Course.java
@Entity 
public class Course {
  @Id 
  @Column (name="course_id")
  public long getId()

  @Basic 
  public String getName()
}

The Student includes the many-to-many relation in its definition.

Student.java
@Entity 
public class Student {
  @Id 
  @Column (name="student_id")
  public long getId()

  @Basic 
  public String getName()

  @ManyToMany(targetEntity="Course") 
  @JoinTable (
      table=@Table (name="student_course_map"),
      joinColumns=@JoinColumn (name="student_id")",
      inverseJoinColumns=@JoinColumn (name="course_id")")
  public Collection getCourses()
}

@ManyToMany

The @ManyToMany annotation marks a collection-valued field as a many-to-many relation. The targetEntity value specifies the target of the relation.

Since the many-to-many relation is a three-table relation, it needs to specify the association table as well as the columns.

Client

ManyToManyServlet.java
  private void doService(PrintWriter out)
    throws java.io.IOException
  {
    PrintWriter out = res.getWriter();

    res.setContentType("text/html");

    Query allStudent = _entityManager.createQuery("SELECT o FROM Student o");
    
    List students = allStudent.listResults();

    for (int i = 0; i < students.size(); i++) {
      Student student = (Student) students.get(i);

      out.println("<h3>" + student.getName() + "</h3>");

      Collection courses = student.getCourses();

      out.println("<ul>");
      Iterator iter = courses.iterator();
      while (iter.hasNext()) {
	Course course = (Course) iter.next();

	out.println("<li>" + course.getName());
      }
      out.println("</ul>");
    }
  }
}

Try the Tutorial


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