| |||||||||||||||||||||||||||||||
Find this tutorial in: /usr/local/resin/webapps/resin-doc/amber/tutorial/cmp-inherit
Try the Tutorial 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.
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 column distinguishes between Students and Prefects. A of "student" creates a Student bean and a of "prefect" creates a Prefect bean.
@InheritanceThe @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. @DiscriminatorColumnBoth 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".
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.
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.
|