Lab#SB04-1: JPA Inherence
Spring Boot JPA Inherence strategies
Welcome to the cifojava2022-6 wiki!
1 Spring Boot Projects JPA Inherence strategies
| Name | Type | Description | Link |
|---|---|---|---|
| universalPlanes | Server | Basic Spring Boot Project | Done |
| JpaInherenceLibrary | Server-Inherence | Inhrence with JPA and UML @Entity |
Done |
| JpaInherenceLibrary1 | Server-Inherence | Inhrence with JPA and UML MappedSuperClass |
Done |
| JpaInherenceLibrary2 | Server-Inherence | Inhrence with JPA and UML Single Table with Discriminator |
Done |
| JpaInherenceLibrary3 | Server-Inherence | Inhrence with JPA and UML Joined Table |
Done |
| JpaInherenceLibrary4 | Server-Inherence | Inhrence with JPA and UML Table per class |
Done |
- there is no
@Serviceneither@Controller - so, they are basically back-end projects
- all work with
@CrudRepository
2 Inhrence with JPA and UML
Inheritance is a fundamental concept of POO, but Relational databases have no concept of inheritance neither NoSQL (MongoDB, DymamoDB), so persisting inheritance in a SQL and NoSQL database _has its own particular way.
Because relational databases have no concept of inheritance, there is no standard way of implementing inheritance in database, so the hardest part of persisting inheritance is choosing how to represent the inheritance in the database.
JPA defines several inheritance mechanisms, mainly defined though the @Inheritance annotation or the <inheritance> element.
There are three inheritance strategies defined from the InheritanceType enum:
- SINGLE_TABLE
- TABLE_PER_CLASS
- JOINED
- Single table inheritance is the default with discriminator values,
- and table per class is an optional feature of the JPA spec, so not all providers may support it.
- in joined strategy each class in the hierarchy is mapped to its table.
MAPPED SUPERCLASS
- JPA also defines a mapped superclass concept defined through the @MappedSuperclass annotation or the
<mapped-superclass>element. - A mapped superclass is not a persistent class, but allows common mappings to be defined for its subclasses.
Links: orientdb, logicbig, Java Persistence/Inheritance wikibooks and apache
2.1 (0) @Entity Inhrence JPA
JpaInherenceLibrary0
- Base project:
- POM
- @Entity:
Book(SuperClass),ItemBook,RareBookandAuhtor n:m:Book<>Auhtor- DataBase H2: application.properties
- Command Line Runner with methods to test
- @CrudRepository JPA 2.0, @Component (
CommandLineRunner) and @Test (Jupiter)
- New Topics
- How to code
inherenceand JPA uses SINGLE_TABLE strategy by default
- How to code
@Entity
public class Book {}
@Entity(name="BookItem")
public class ItemBook extends Book {
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "AUTHOR_BOOK_JOIN_TABLE",
joinColumns = { @JoinColumn(name = "AUTHOR_FK" )},
inverseJoinColumns = { @JoinColumn(name = "BOOK_FK" )})
private Set<Author>authors = new HashSet<Author>();
}
@Entity(name="RareItem")
public class RareItem extends Book {}
@Entity(name="Author")
@Table(name="AUTHOR_TABLE")
public class Author {}- Versions
- JpaInherenceLibrary v 1.1 : without @MappedSuperClass and @Entity superclass, Single Table strategy is applied BY DEFAULT
2.2 (1) MappedSuperclass Inhrence JPA
JpaInherenceLibrary1
- Base project:
- POM
- @Entity:
ItemBook,RareBookandAuhtor - Non-@Entity:
Book - DataBase H2: application.properties
- Command Line Runner with methods to test
- @CrudRepository JPA 2.0, @Component (
CommandLineRunner) and @Test (Jupiter)
- New Topics
- How can not we code
@MappedSuperclassand@ManyToManyauthor
- How can not we code
@MappedSuperclass
public abstract class Book {}
@Entity(name="BookItem")
@Table(name="ITEM_BOOK_TABLE")
public class ItemBook extends Book {
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "AUTHOR_BOOK_JOIN_TABLE",
joinColumns = { @JoinColumn(name = "AUTHOR_FK" )},
inverseJoinColumns = { @JoinColumn(name = "BOOK_FK" )})
private Set<Author>authors = new HashSet<Author>();
}
@Entity(name="RareItem")
@Table(name="RARE_ITEM_TABLE")
public class RareItem extends Book {}
@Entity(name="Author")
@Table(name="AUTHOR_TABLE")
public class Author {}With @MappedSuperclass any relationship can’t be done
With @MappedSuperclass, one SuperClass and two SubClasses:
Versions
- JpaInherenceLibrary1 v 1.0 : not using
authorjust inherence without relatonship @ManyToMany
- JpaInherenceLibrary1 v 1.0 : not using
2.3 (2) Single Table with Discriminator Inhrence JPA
JpaInherenceLibrary2
- Base project:
- POM
- @Entity:
Book(SuperClass),ItemBook,RareBookandAuhtor n:m:Book<>Auhtor- DataBase H2 : application.properties
- Command Line Runner with methods to test
- @CrudRepository JPA 2.0, @Component (
CommandLineRunner) and @Test (Jupiter)
- New Topics
- How to code
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
- How to code
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="BOOK_TYPE",
discriminatorType = DiscriminatorType.STRING)
public class Book {}
@Entity(name="BookItem")
@Table(name="BOOK_ITEM_TABLE")
@DiscriminatorValue(value= "ITEMBOOK")
public class BookItem extends Book {
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "AUTHOR_BOOK_JOIN_TABLE",
joinColumns = { @JoinColumn(name = "AUTHOR_FK" )},
inverseJoinColumns = { @JoinColumn(name = "BOOK_FK" )})
private Set<Author>authors = new HashSet<Author>();}
@Entity(name="RareItem")
@Table(name="RARE_ITEM_TABLE")
@DiscriminatorValue(value= "REAREBOOK")
public class RareItem extends Book {}
@Entity(name="Author")
@Table(name="AUTHOR_TABLE")
public class Author {}With @Inheritance and SINGLE_TABLE

version 1.1 : JpaInherenceLibrary2, SINGLE_TABLE strategy
2.4 (3) Joined Table Inhrence JPA
JpaInherenceLibrary3
- Base project:
- POM
- @Entity:
Book(SuperClass),ItemBook,RareBookandAuhtor n:m:Book<>Auhtor- DataBase H2 : application.properties
- Command Line Runner with methods to test
- @CrudRepository JPA 2.0, @Component (
CommandLineRunner) and @Test (Jupiter)
- New Topics
- How to code
@Inheritance(strategy = InheritanceType.JOINED)
- How to code
@Entity
@Table(name="BOOK_TABLE")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="BOOK_TYPE",
discriminatorType = DiscriminatorType.STRING)
public class Book {}
@Entity(name="BookItem")
@Table(name="BOOK_ITEM_TABLE")
@PrimaryKeyJoinColumn(name = "bookItemId")
@DiscriminatorValue(value= "ITEMBOOK")
public class BookItem extends Book {
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "AUTHOR_BOOK_JOIN_TABLE",
joinColumns = { @JoinColumn(name = "AUTHOR_FK" )},
inverseJoinColumns = { @JoinColumn(name = "BOOK_FK" )})
private Set<Author>authors = new HashSet<Author>();}
@Entity(name="RareItem")
@Table(name="RARE_ITEM_TABLE")
@PrimaryKeyJoinColumn(name = "rareItemId")
@DiscriminatorValue(value= "RAREBOOK")
public class RareItem extends Book {}
@Entity(name="Author")
@Table(name="AUTHOR_TABLE")
public class Author {}With @Inheritance and JOINED

2.5 (4) Table per class Inhrence JPA
JpaInherenceLibrary4
- Base project:
- POM
- @Entity:
Book(SuperClass),ItemBook,RareBookandAuhtor n:m:Book<>Auhtor- DataBase H2 : application.properties
- Command Line Runner with methods to test
- @CrudRepository JPA 2.0, @Component (
CommandLineRunner) and @Test (Jupiter)
- New Topics
- How to code
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
- How to code
@Entity
@Table(name="BOOK_TABLE")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Book {}
@Entity(name="BookItem")
@Table(name="BOOK_ITEM_TABLE")
public class BookItem extends Book {
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "AUTHOR_BOOK_JOIN_TABLE",
joinColumns = { @JoinColumn(name = "AUTHOR_FK" )},
inverseJoinColumns = { @JoinColumn(name = "BOOK_FK" )})
private Set<Author>authors = new HashSet<Author>();}
@Entity(name="RareItem")
@Table(name="RARE_ITEM_TABLE")
public class RareItem extends Book {}
@Entity(name="Author")
@Table(name="AUTHOR_TABLE")
public class Author {}- With @Inheritance and TABLE_PER_CLASS
2.6 (0) Basic Spring Boot project
Universal Planes
- Preliminary works:
- Create project on Spring Init
- version 1.0 : spring boot executions



