Mapping Hierarchy
1.Single Table
With JPA it is possible to find different approaches for persisting class hierarchies. In an Object Orientated language
like Java it is very easy to find hierarchy between classes that will have their data persisted.
The Single Table strategy will store all hierarchy data into one table. Check the code below:
import javax.persistence.*;
@Entity
@Table(name = "DOG")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DOG_CLASS_NAME")
public abstract class Dog {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
// get and set
}
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("SMALL_DOG")
public class SmallDog extends Dog {
private String littleBark;
public String getLittleBark() {
return littleBark;
}
public void setLittleBark(String littleBark) {
this.littleBark = littleBark;
}
}
import javax.persistence.*;
@Entitye
@DiscriminatorValue("HUGE_DOG")
public class HugeDog extends Dog {
private int hugePooWeight;
public int getHugePooWeight() {
return hugePooWeight;
}
public void setHugePooWeight(int hugePooWeight) {
this.hugePooWeight = hugePooWeight;
}
}
About the code above:
•@Inheritance(strategy = InheritanceType.SINGLE_TABLE) => this annotation should be placed
in the highest place of the hierarchy (the “father” class), also known as “root”. This annotation
will define the hierarch pattern to be followed, in the annotation above the hierarchy mapping
strategy is set to Single Table.
•@DiscriminatorColumn(name = “DOG_CLASS_NAME”) => will define the name of the column
that will link a database table row to a class. Check in the image below how the data is stored.
•@DiscriminatorValue => Will set the value to be persisted in the column defined in the
annotation @DiscriminatorColumn.
2. Joined
When a hierarchy is using the Joined strategy each entity will have its data stored in the respective table. Instead of using just one table for all hierarchy classes, each entity will have its own table.
Check the code below to see how Joined strategy can be used:
import javax.persistence.*;
@Entity
@Table(name = "DOG")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "DOG_CLASS_NAME")
public abstract class Dog {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
// get and set
}
import javax.persistence.*;
@Entity
@DiscriminatorValue("HUGE_DOG")
public class HugeDog extends Dog {
private int hugePooWeight;
public int getHugePooWeight() {
return hugePooWeight;
}
public void setHugePooWeight(int hugePooWeight) {
this.hugePooWeight = hugePooWeight;
}
}
import javax.persistence.*;
@Entity
@DiscriminatorValue("SMALL_DOG")
public class SmallDog extends Dog {
private String littleBark;
public String getLittleBark() {
return littleBark;
}
public void setLittleBark(String littleBark) {
this.littleBark = littleBark;
}
}
About the code above:
•The annotation @Inheritance(strategy = InheritanceType.JOINED) now has the Joined value.
Every entity has its information persisted across unique tables; for this strategy JPA will use one table per entity regardlessthe entity being concrete or abstract.
The Dog table maintains all data common to all the classes of the hierarchy; The Dog table maintains a column indicating to which entity a row belongs to.
3. Table per Concrete Class:
The Table Per Concrete strategy will create a table per concrete entity. If an abstract entity is found in the hierarchy
this data will be persisted in the concrete children entities database tables.
Check the code below:
import javax.persistence.*;
@Entity
@Table(name = "DOG")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Dog {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
// get and set
}
import javax.persistence.Entity;
@Entity
public class HugeDog extends Dog {
private int hugePooWeight;
public int getHugePooWeight() {
return hugePooWeight;
}
public void setHugePooWeight(int hugePooWeight) {
this.hugePooWeight = hugePooWeight;
}
}
import javax.persistence.Entity;
@Entity
public class SmallDog extends Dog {
private String littleBark;
public String getLittleBark() {
return littleBark;
}
public void setLittleBark(String littleBark) {
this.littleBark = littleBark;
}
}
About the code above:
•@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) => define the hierarchy type as
table per class.
•The annotation @DiscriminatorColumn(name = “DOG_CLASS_NAME”) is not used in the children
classes anymore. Each concrete class will have its own data, JPA will not spread entity data
across tables.
•There is no need for the annotation @DiscriminatorValue for the same principal as above.
NEXT
1.Single Table
With JPA it is possible to find different approaches for persisting class hierarchies. In an Object Orientated language
like Java it is very easy to find hierarchy between classes that will have their data persisted.
The Single Table strategy will store all hierarchy data into one table. Check the code below:
import javax.persistence.*;
@Entity
@Table(name = "DOG")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DOG_CLASS_NAME")
public abstract class Dog {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
// get and set
}
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("SMALL_DOG")
public class SmallDog extends Dog {
private String littleBark;
public String getLittleBark() {
return littleBark;
}
public void setLittleBark(String littleBark) {
this.littleBark = littleBark;
}
}
import javax.persistence.*;
@Entitye
@DiscriminatorValue("HUGE_DOG")
public class HugeDog extends Dog {
private int hugePooWeight;
public int getHugePooWeight() {
return hugePooWeight;
}
public void setHugePooWeight(int hugePooWeight) {
this.hugePooWeight = hugePooWeight;
}
}
About the code above:
•@Inheritance(strategy = InheritanceType.SINGLE_TABLE) => this annotation should be placed
in the highest place of the hierarchy (the “father” class), also known as “root”. This annotation
will define the hierarch pattern to be followed, in the annotation above the hierarchy mapping
strategy is set to Single Table.
•@DiscriminatorColumn(name = “DOG_CLASS_NAME”) => will define the name of the column
that will link a database table row to a class. Check in the image below how the data is stored.
•@DiscriminatorValue => Will set the value to be persisted in the column defined in the
annotation @DiscriminatorColumn.
2. Joined
When a hierarchy is using the Joined strategy each entity will have its data stored in the respective table. Instead of using just one table for all hierarchy classes, each entity will have its own table.
Check the code below to see how Joined strategy can be used:
import javax.persistence.*;
@Entity
@Table(name = "DOG")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "DOG_CLASS_NAME")
public abstract class Dog {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
// get and set
}
import javax.persistence.*;
@Entity
@DiscriminatorValue("HUGE_DOG")
public class HugeDog extends Dog {
private int hugePooWeight;
public int getHugePooWeight() {
return hugePooWeight;
}
public void setHugePooWeight(int hugePooWeight) {
this.hugePooWeight = hugePooWeight;
}
}
import javax.persistence.*;
@Entity
@DiscriminatorValue("SMALL_DOG")
public class SmallDog extends Dog {
private String littleBark;
public String getLittleBark() {
return littleBark;
}
public void setLittleBark(String littleBark) {
this.littleBark = littleBark;
}
}
About the code above:
•The annotation @Inheritance(strategy = InheritanceType.JOINED) now has the Joined value.
Every entity has its information persisted across unique tables; for this strategy JPA will use one table per entity regardlessthe entity being concrete or abstract.
The Dog table maintains all data common to all the classes of the hierarchy; The Dog table maintains a column indicating to which entity a row belongs to.
3. Table per Concrete Class:
The Table Per Concrete strategy will create a table per concrete entity. If an abstract entity is found in the hierarchy
this data will be persisted in the concrete children entities database tables.
Check the code below:
import javax.persistence.*;
@Entity
@Table(name = "DOG")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Dog {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
// get and set
}
import javax.persistence.Entity;
@Entity
public class HugeDog extends Dog {
private int hugePooWeight;
public int getHugePooWeight() {
return hugePooWeight;
}
public void setHugePooWeight(int hugePooWeight) {
this.hugePooWeight = hugePooWeight;
}
}
import javax.persistence.Entity;
@Entity
public class SmallDog extends Dog {
private String littleBark;
public String getLittleBark() {
return littleBark;
}
public void setLittleBark(String littleBark) {
this.littleBark = littleBark;
}
}
About the code above:
•@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) => define the hierarchy type as
table per class.
•The annotation @DiscriminatorColumn(name = “DOG_CLASS_NAME”) is not used in the children
classes anymore. Each concrete class will have its own data, JPA will not spread entity data
across tables.
•There is no need for the annotation @DiscriminatorValue for the same principal as above.
NEXT
No comments:
Post a Comment