Showing posts with label Class. Show all posts
Showing posts with label Class. Show all posts

Wednesday, 22 August 2012

JPA Inheritance Types - Joined, Single Table, Table Per Class

There are three strategies to store data of classes inheriting from each other in JPA. This is defined in the root classes of inheritance structures with the @Inheritance annotation.

Joined

The joined strategy creates a table for the root class of an inheritance structure, and a separate table for each inheriting class in the structure. The data of the root class table is not copied in the inheriting class tables.

This is a root class example:
@Entity
@AutoProperty
@Inheritance(strategy=InheritanceType.JOINED)
public class Joined implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    private String s;

    // Setter, Getters, Constructors, Pojomatic...

}

Single Table

The single table strategy creates a unique table for a given inheritance structure where all classes in the hierarchy are saved. This table contains a discriminating column which will help differentiate between the different classes. Each class must define a discriminating value with @DiscriminatorValue.

For the root class:
@Entity
@AutoProperty
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="discr", discriminatorType=DiscriminatorType.INTEGER)
@DiscriminatorValue("44")
public class SingleTable implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    private String s;

    // Setter, Getters, Constructors, Pojomatic...

}
For an inheriting class:
@Entity
@AutoProperty
@DiscriminatorValue("45")
public class InheritsSingleTable extends SingleTable {

    ...

}

Table Per Class

The table per class strategy create a separate table per class.

Root class example:
@Entity
@AutoProperty
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class TablePerClass implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private long id;
 
    private String s;

    // Setter, Getters, Constructors, Pojomatic...

}
Notice that one must use a TABLE generation type for the Id.

Check

The following:
JPA.INSTANCE.clear();

InheritsJoined ij = new InheritsJoined();
ij.setS("IJ1");
ij.setS2("IJ2");

JPA.INSTANCE.save(ij);

InheritsSingleTable st
    = new InheritsSingleTable();
st.setS("ST1");
st.setS2("ST2");

JPA.INSTANCE.save(st);

InheritsTablePerClass tpc
    = new InheritsTablePerClass();
tpc.setS("TPC1");
tpc.setS2("TPC2");

JPA.INSTANCE.save(tpc);

JPA.INSTANCE.clear();

InheritsJoined retr_ij = JPA.INSTANCE.get(
    InheritsJoined.class, ij.getId());
System.out.println("Source == Retrieved: " + (ij==retr_ij));
System.out.println(retr_ij);

InheritsSingleTable retr_st = JPA.INSTANCE.get(
    InheritsSingleTable.class, st.getId());
System.out.println("Source == Retrieved: " + (st==retr_st));
System.out.println(retr_st);

InheritsTablePerClass retr_tpc = JPA.INSTANCE.get(
    InheritsTablePerClass.class, tpc.getId());
System.out.println("Source == Retrieved: " + (tpc==retr_tpc));
System.out.println(retr_tpc);
Generates the following output:
Source == Retrieved: false
InheritsJoined{id: {1}, s: {IJ1}, s2: {IJ2}}
Source == Retrieved: false
InheritsSingleTable{id: {1}, s: {ST1}, s2: {ST2}}
Source == Retrieved: false
InheritsTablePerClass{id: {1}, s: {TPC1}, s2: {TPC2}}
The above example is available from Github in the JPA directory. It relies on Pojomatic too. Some errors messages will be displayed because of a known and harmless issue.

JPA Inheritance - Non Entity Super Class

An entity can inherit of a non-entity super class. The consequence is that the non-entity class data is not persisted.

For example:
@AutoProperty
public class NonEntitySuperClass {

 private String s;

    // Setter, Getters, Constructors, Pojomatic...

}
A class extends this non-entity class:
@Entity
@AutoProperty
public class InheritingNonEntitySuperClass
    extends NonEntitySuperClass
        implements Serializable {
 
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
 
    private String s2;

    // Setter, Getters, Constructors...

}
The following code:
JPA.INSTANCE.clear();

InheritingNonEntitySuperClass insc
    = new InheritingNonEntitySuperClass();
insc.setS("P1P");
insc.setS2("AZ3");
  
JPA.INSTANCE.save(insc);
JPA.INSTANCE.clear();
  
InheritingNonEntitySuperClass retr = JPA.INSTANCE.get(
    InheritingNonEntitySuperClass.class, insc.getId());

System.out.println("Source == Retrieved: " + (insc==retr));
System.out.println(retr);
Generates the following output:
Source == Retrieved: false
InheritingNonEntitySuperClass{s: {null}, id: {1}, s2: {AZ3}}
Notice that the s value is null, instead of P1P.

The above example is available from Github in the JPA directory. It relies on Pojomatic too. Some errors messages will be displayed because of a known and harmless issue.

Saturday, 11 August 2012

Java Abstract Class vs Interface Pros and Con's

This a quick reminder about pros and con's of Java abstract classes and interfaces:

Interface

  • A class can implement multiple interfaces
  • An interface cannot provide any code at all
  • An interface can only define public static final constants
  • An interface cannot define instance variables
  • Adding a new method has ripple effects on implementing classes (design maintenance)
  • JAXB cannot deal with interfaces
  • An interface cannot extends or implement an abstract class
  • All interface methods are public
In general, interfaces should be used to define contracts (what is to be achieved, not how to achieve it).

Abstract Class

  • A class can extend at most one abstract class
  • An abstract class can contain code
  • An abstract class can define both static and instance constants (final)
  • An abstract class can define instance variables
  • Modification of existing abstract class code has ripple effects on extending classes (implementation maintenance)
  • Adding a new method to an abstract class has no ripple effect on extending classes
  • An abstract class can implement an interface
  • Abstract classes can implement private and protected methods
Abstract classes should be used for (partial) implementation. They can be a mean to restrain the way API contracts should be implemented.