Skip to content
Albert edited this page Jun 9, 2022 · 45 revisions

Welcome to the cifojava2022-6 wiki!

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 @Service neither @Controller
  • so, they are basically back-end projects
  • all work with @CrudRepository

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:

  1. SINGLE_TABLE
  2. TABLE_PER_CLASS
  3. 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.

Entity Inheritance

Links: orientdb, logicbig, Java Persistence/Inheritance wikibooks and apache

(0) @Entity Inhrence JPA

JpaInherenceLibrary0
  • Base project:

    • POM
    • @Entity: Book (SuperClass), ItemBook, RareBook and Auhtor
    • 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 inherence and JPA uses SINGLE_TABLE strategy by default

        @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

(1) MappedSuperclass Inhrence JPA

JpaInherenceLibrary1
  • Base project:

    • POM
    • @Entity: ItemBook, RareBook and Auhtor
    • 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 @MappedSuperclass and @ManyToMany author

        @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

    • With @MappedSuperclass, one SuperClass and two SubClasses:

      With @MappedSuperclass

  • Versions

(2) Single Table with Discriminator Inhrence JPA

JpaInherenceLibrary2
  • Base project:

    • POM
    • @Entity: Book (SuperClass), ItemBook, RareBook and Auhtor
    • 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)

         @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

    @Inheritance

version 1.1 : JpaInherenceLibrary2, SINGLE_TABLE strategy

(3) Joined Table Inhrence JPA

JpaInherenceLibrary3
  • Base project:

    • POM
    • @Entity: Book (SuperClass), ItemBook, RareBook and Auhtor
    • 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)

        @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

    @Inheritance JOINED

verison 1.1 : JpaInherenceLibrary3

(4) Table per class Inhrence JPA

JpaInherenceLibrary4
  • Base project:

    • POM
    • @Entity: Book (SuperClass), ItemBook, RareBook and Auhtor
    • 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)

        @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

@Inheritance TABLE PER CLASS

version 1.1 : JpaInherenceLibrary4


(0) Basic Spring Boot project

Universal Planes