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

cmp inheritance

Amber supports database-backed inheritance, allowing for persistent-backed polymorphism and more sophisticated object-oriented modelling.

The example uses a single table to represent both Student and Prefect values. The "type" column serves as a discriminator to select the proper type.


Files in this tutorial

WEB-INF/resin-web.xmlresin-web.xml configuration
WEB-INF/classes/META-INF/persistence.xmlMETA-INF/persistence.xml configuration
WEB-INF/classes/example/Student.javaThe student bean
WEB-INF/classes/example/Prefect.javaThe prefect bean
WEB-INF/classes/example/QueryServlet.javaThe course servlet

Database Schema

CREATE TABLE amber_inherit_student (
  id BIGINT PRIMARY KEY auto_increment,

  type VARCHAR(10),

  name VARCHAR(250)

INSERT INTO amber_inherit_student VALUES(1, 'student', 'Harry Potter')
INSERT INTO amber_inherit_student VALUES(2, 'prefect', 'Ron Weasley')
INSERT INTO amber_inherit_student VALUES(1, 'prefect', 'Hermione Granger')

Bean Implementation

The example has two beans to illustrate inheritance. The Student bean is the base class and represents all students. Some students are also prefects. The prefect students are represented by a Prefect class.

In the database, the type column distinguishes between Students and Prefects. A type of "student" creates a Student bean and a type of "prefect" creates a Prefect bean.
package example;

public class Student {
  private long _id;

  private String _name;

  public Student()

  public Student(String name)
    _name = name;

  public String getName()
    return _name;

  public String toString()
    return "Student[_name]";


The @Inheritance annotation marks the entity bean as supporting inheritance.

Amber supports two inheritance types: SINGLE_TABLE and JOINED. SINGLE_TABLE uses a single table for all classes and JOINED adds additional tables for child classes. Both types use a discriminator column in the base table to mark the Java type of the database entry. SINGLE_TABLE is the default.

This example uses the default SINGLE_TABLE since there's no additional information for the Prefect. The example only uses inheritance to illustrate the database polymorphism.

The values in the discriminator column determine the Java class. Each entity will specify its discriminator value in the discriminatorValue annotation. In the example, the Student class has a "student" value and the Prefect class has a "prefect" value.


Both SINGLE_TABLE and JOINED use a discriminator column to distinguish the classes. The @DiscriminatorColumn annotation specifies the column name. In this example, the column is "type". The default discriminator column is "TYPE".
package example;

import static javax.ejb.AccessType.FIELD;

public class Prefect {
  public Prefect()

  public Prefect(String name)

  public String toString()
    return "Prefect[" + getName() + "]";

In this example, the Prefect class has no additional columns, so it only specifies the @Entity and @Inheritance values. When Resin loads the objects from the database, it will instantiate the proper type.

Client Servlet

The client code using inheritance is no different from normal queries. The Query will perform any necessary database joins to return the proper Java class. In this case, prefects will return Prefect objects and students will return Student objects.
public void service(PrintWriter out)
  throws IOException

  Query query = _manager.createQuery("SELECT o FROM Student o");
    for (Object student : query.listResults()) {
      out.println("student: " + student() + "<br>");
Student[Harry Potter]
Prefect[Hermione Granger]
Prefect[Ron Weasley]


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