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

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

The Amber Query API resembles the JDBC PreparedStatement with enhanced SQL and direct support for objects.

  1. Files in this tutorial
  2. Database Schema
  3. Query

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/House.java The house bean
WEB-INF/classes/example/QueryServlet.java The course servlet

Database Schema

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

  @Basic 
  public String getName()

  @OneToMany (targetEntity=Student.class,
        mappedBy="house")
  public Collection getStudentList()
}

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

  @Basic 
  public String getName()

  @ManyToOne 
  @JoinColumn (name="house")
  public House getHouse()
}

Query

QueryServlet.java
  private void doService(PrintWriter out)
    throws java.io.IOException
  {
    Query allHouse = _entityManager.createQuery("SELECT o FROM House o");
    
    String sql = ("SELECT s" +
		  " FROM House h, IN(h.studentList) s" +
		  " WHERE h.id=?1 AND s.gender='M'");
    Query boysInHouse = _entityManager.createQuery(sql);
    
    List houses = allHouse.getResultList();

    for (int i = 0; i < houses.size(); i++) {
      House house = (House) houses.get(i);
      
      out.println("<H3>Boys living in " + house.getName() + ":</H3>");

      boysInHouse.setParameter(1, new Long(house.getId()));
      List boys = boysInHouse.getResultList();

      if (boys.size() == 0)
	out.println("No boys are living in " + house.getName());

      for (int j = 0; j < boys.size(); j++) {
	Student boy = (Student) boys.get(j);

	out.println(boy.getName() + "<br>");
      }
    }
  }
}

Try the Tutorial


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