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


 

Resin Documentation

home company blog wiki docs 
app server web server 
health cloud java ee pro 
 Resin Server | Application Server (Java EE Certified) and Web Server
 

many-to-many cmp


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

Files in this tutorial

WEB-INF/resin-web.xmlresin-web.xml configuration
WEB-INF/classes/META-INF/persistence.xmlpersistence.xml configuration
WEB-INF/classes/example/Student.javaThe student bean
WEB-INF/classes/example/Course.javaThe course bean
WEB-INF/classes/example/Grade.javaThe grade bean
WEB-INF/classes/example/ManyToManyServlet.javaThe 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>");
    }
  }
}

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

Cloud-optimized Resin Server is a Java EE certified Java Application Server, and Web Server, and Distributed Cache Server (Memcached).
Leading companies worldwide with demand for reliability and high performance web applications including SalesForce.com, CNET, DZone and many more are powered by Resin.

home company blog wiki docs 
app server web server 
health cloud java ee pro