Search code examples
javahibernatehibernate-annotations

Hibernate Common Primary Key part of All Entity


I have following database structure, where some specific column are present on each table and those columns(CPK1,CPK2,CPK3,CPK4) are part of primary key of all tables. Also each table has one extra primary key, which is actual primary key and referenced on another table as foreign key (IDPKA in TableB, IDPKB on TableC). I am not able to create the entity structure for those tables.

TableA
-------------------------------------------------------------
CPK1    CPK2    CPK3    CPK4    IDPKA   A1
-------------------------------------------------------------
10      1       2       3       1       XYZ 
-------------------------------------------------------------

TableB
-------------------------------------------------------------
CPK1    CPK2    CPK3    CPK4    IDPKB   B1  IDPKA
-------------------------------------------------------------
10      1       2       3       1       BDATA1  1
10      1       2       3       2       BDATA2
-------------------------------------------------------------

TableC
-------------------------------------------------------------
CPK1    CPK2    CPK3    CPK4    IDPKC   C1  IDPKB
-------------------------------------------------------------
10      1       2       3       1       ABCD    1
10      1       2       3       2       PQRS    1
10      1       2       3       3       ABCD1   2
10      1       2       3       4       PQRS1   2
-------------------------------------------------------------

as per data following are the relation ship.

XYZ
|
|---BDATA1
|   |
|   |---ABCD    
|   |---PQRS    
|
|---BDATA2
|   |
|   |---ABCD1   
|   |---PQRS1   

Entity Structure should be like that I can Query on Table A with CPK1,CPK2,CPK3,CPK4,IDPKA and all the data should be retrieved with proper relation.

One more thing I am using annotation on entity POJO.

If it is possible please help, if not possible show me some way out please.


Solution

  • Since you're correlating tables with each other with only IDPKA, IDPKB and IDPKC forget about other columns. They seem like just regualar columns for those tables. If you want to improve search, you'll make composite imdexes with those later but it's not our topic now.

    As I understand, both 2 relationships inbetween are one-to-many. Just search a Hibernate tutorial to establish those bindings through annotations.

    A--(1-n)-->B--(1-n)-->C

    Those bindings (lets say left to right) should have fetch = FetchType.EAGER configuration, so you can get all Bs and Cs with one selection for A.

    As expected there is a right to left connection for the mapping which are many-to-one. This mapping can be EAGER or not, it's up to you.

    Important point for EAGER is: If your entities are many-to-many and EAGER is used, it may result in initiating for selecting all DB data. This should always be kept in mind.
    A sample:

    public class A {    
        ...
        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "a")
        @Fetch(FetchMode.SELECT)
        public List<B> getBs() {
            return this.bs;
        }       
        ...
    }
    
    public class B {        
        ...
        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "IDPKA")
        public A getA() {
            return this.a;
        }
        ... 
    }