<document>
  <header>
    <product>resin-ee</product>
    <title>CMP Inheritance</title>
        <description>
          <p>Amber supports database-backed inheritance, allowing
for persistent-backed polymorphism and more sophisticated
object-oriented modelling.</p>

          <p>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.</p>
        </description>
    <type>tutorial</type>
    <tutorial-startpage>query</tutorial-startpage>
  </header>

  <body>
    <summary/>

<section title="Files in this tutorial">
<deftable>
<tr><td><viewfile-link file="WEB-INF/resin-web.xml"/>
    <td>resin-web.xml configuration
<tr><td><viewfile-link file="WEB-INF/classes/META-INF/persistence.xml"/>
    <td>META-INF/persistence.xml configuration
<tr><td><viewfile-link file="WEB-INF/classes/example/Student.java"/>
    <td>The student bean
<tr><td><viewfile-link file="WEB-INF/classes/example/Prefect.java"/>
    <td>The prefect bean
<tr><td><viewfile-link file="WEB-INF/classes/example/QueryServlet.java"/>
    <td>The course servlet
</deftable>
</section>

<section title="Database Schema">

<example title="student.sql">
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')
</example>

</section>

<section title="Bean Implementation">

<p>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.</p>

<p>In the database, the <var>type</var> column distinguishes between
Students and Prefects.  A <var>type</var> of "student" creates a
Student bean and a <var>type</var> of "prefect" creates a Prefect bean.</p>

<example title="Student.java">
package example;

<a href="amber|cmp-table.xtp#@Entity">@javax.ejb.Entity</a>(access=FIELD)
<a href="amber|cmp-table.xtp#@Table">@javax.ejb.Table</a>(name="ejb3_inherit_student")
<a href="amber|cmp-table.xtp#@Inheritance">@javax.ejb.Inheritance</a>(discriminatorValue="student")
<a href="amber|cmp-table.xtp#@DiscriminatorColumn">@javax.ejb.DiscriminatorColumn</a>(name="type")
public class Student {
  <a href="ejb3|cmp-table.xtp#@Id">@javax.ejb.Id</a>(generator=AUTO)
  <a href="ejb3|cmp-table.xtp#@Column">@javax.ejb.Column</a>(name="id")
  private long _id;

  <a href="ejb3|cmp-table.xtp#@Basic">@javax.ejb.Basic</a>
  <a href="ejb3|cmp-table.xtp#@Column">@javax.ejb.Column</a>(name="name")
  private String _name;

  public Student()
  {
  }

  public Student(String name)
  {
    _name = name;
  }

  public String getName()
  {
    return _name;
  }

  public String toString()
  {
    return "Student[_name]";
  }
}
</example>

<section title="@Inheritance">

<p>The <a href="amber|cmp-table.xtp#@Inheritance">@Inheritance</a>
annotation marks the entity bean as supporting inheritance.</p>

<p>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.</p>

<p>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.</p>

<p>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.</p>

</section>

<section title="@DiscriminatorColumn">

<p>Both SINGLE_TABLE and JOINED use a discriminator column to
distinguish the classes.  The <a
href="ejb3|cmp-table.xtp#@DiscriminatorColumn">@DiscriminatorColumn</a>
annotation specifies the column name.  In this example, the column is
"type".  The default discriminator column is "TYPE".</p>

</section>

<example title="Prefect.java">
package example;

import static javax.ejb.AccessType.FIELD;

<a href="ejb3|cmp-table.xtp#@Entity">@javax.ejb.Entity</a>(access=FIELD)
<a href="ejb3|cmp-table.xtp#@Inheritance">@javax.ejb.Inheritance</a>(discriminatorValue="prefect")
public class Prefect {
  public Prefect()
  {
  }

  public Prefect(String name)
  {
    super(name);
  }

  public String toString()
  {
    return "Prefect[" + getName() + "]";
  }
}
</example>

<p>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.
</p>

</section>

<section title="Client Servlet">

<p>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.</p>

<example title="QueryServlet.java">
public void service(PrintWriter out)
  throws IOException
{
  out.println("&lt;h3>Students&lt;/h3>");

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

</section>

  </body>
</document>
