Data JPA-Collections Mapping

My alt text
*) For every collection one child table is created *) This child table contains at max 3 columns Key Column - FK Column Index Column - Position (only for List/Map) Element Column -- Data *) @ElementCollection Should be provided over collecton variable. *) To provide child table details created for collection variable like table name, FK(Join Column)..etc @CollectionTable( name="", joinColumns = @JoinColumn(name="") ) *) Element Column name : @Column(name="") *) For List Collection we should add : @OrderColumn(name="") List stores data in indexs given by java (0,1,2,3...) *) For Map Collection we should add : @MapKeyColumn(name="") Map - Key behaves like index Map - value behaves like element column. ---------code------------------------------------------------------- Name: SpringBoot2DataJpaCollectionsEx Dep : Data JPA, MySQL, Lombok 1. Model class package in.nareshit.raghu.model; import java.util.List; import java.util.Map; import java.util.Set; import javax.persistence.CollectionTable; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.MapKeyColumn; import javax.persistence.OrderColumn; import javax.persistence.Table; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor @Entity @Table(name="emp_tab") public class Employee { @Id @Column(name="eid") private Integer empId; @Column(name="ename") private String empName; @Column(name="esal") private Double empSal; //collection variables @ElementCollection @CollectionTable( name="emp_prjs_tab", joinColumns = @JoinColumn(name="eid") ) @Column(name="proj") private Set empPrjs; @ElementCollection @CollectionTable( name="emp_modules_tab", joinColumns = @JoinColumn(name="eid") ) @OrderColumn(name="epos") @Column(name="module") private List empModules; @ElementCollection @CollectionTable( name="emp_clients_tab", joinColumns = @JoinColumn(name="eid") ) @MapKeyColumn(name="cindex") @Column(name="cname") private Map<String,String> empClients; } 2. Repository package in.nareshit.raghu.repo; import; import in.nareshit.raghu.model.Employee; public interface EmployeeRepository extends JpaRepository<Employee, Integer> { } 3. Runner class package in.nareshit.raghu.runner; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import in.nareshit.raghu.model.Employee; import in.nareshit.raghu.repo.EmployeeRepository; @Component public class TestRunner implements CommandLineRunner { @Autowired private EmployeeRepository repo; public void run(String... args) throws Exception { new Employee(10, "A", 3.3, Set.of("P1","P2","P3"), List.of("M1","M2"), Map.of("C1","NIT","C2","ORCL") ) ); System.out.println("DONE"); } } *) proeprties file #datasource spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/boot8pm spring.datasource.username=root spring.datasource.password=root #JPA spring.jpa.hibernate.ddl-auto=create spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect ---------------------------------------------------------- @ElementCollection (must) --child table -- table name @CollectionTable(name="") KeyColumn - @JoinColum(name="") Index Column List - @OrderColumn(name="") Map - @MapKeyColumn(name="") Element Column - @Column(name="") ----------------------------------------------------------- Task: Student sid sname sfee marks:List subnames:Set grades:Map<String,String> *) Child table created for collection never contains extra columns this is not association mapping. *) If we do not provide Collectiontable and JoinColumn then tablename is = className_variableName Join Column Name is = className_pkColumnName ie Providing @CollectionTable(name="__", joinColumns = @JoinColumn(name="__")) are optional. @JoinColumn(name="") -- FK Column created for linking other table PK ================================================= class mapped with table variable --- column collection variable --- childtable object -- row