Home > Software engineering >  Fetch records from Multiple tables having different type of primary key. Hibernate
Fetch records from Multiple tables having different type of primary key. Hibernate

Time:01-15

I have a use case where i need to get records from different tables based on emailAddress, and each of these tables have different type of primary key(String ,Integer etc)

@Entity
Class A{
@Id
Integer ID;
String emailAddress;
}

@Entity
Class  B{
@Id
String ID;
String emailAddress;
}

@Entity
Class C{
@Id
Long ID;
String emailAddr;
}

Now lets say i have email address : [email protected]

My use case if to fetch Records associated with this email address from each of these tables and do some processing on the primary Key.

something like

for each table
   get list records with the given emailAddress from that table
   do some processing on primary key

My main problem is fetching the id of the result, since it is of different type for each table. I want do it in a loop.

I'm using JpaRepository for fetching data from database

CodePudding user response:

You can use Java type casting where needed, to do specific operations. Firstly you bring all your three entity classes into one umbrella. Make then child class of a same parent class.

package com.solution.domain;

public class Domain {
}

Entity Class A

package com.solution.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class A extends Domain{
    @Id
    private Integer id;
    @Column (name="EMAILADDRESS")
    private String emailAddress;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }


}

Entity Class B

package com.solution.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class B extends Domain{
    @Id
    String ID;
    @Column (name="EMAILADDRESS")
    String emailAddress;

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
}

Entity Class C

package com.solution.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
public class C extends Domain{
    @Id
    Long ID;
   @Column (name="EMAILADDRESS")
    String emailAddress;

    public Long getID() {
        return ID;
    }

    public void setID(Long ID) {
        this.ID = ID;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    @Override
    public String toString() {
        return "C{"  
                "ID="   ID  
                ", emailAddress='"   emailAddress   '\''  
                '}';
    }
}

Class to access all three JPA Repos collate results and sends a list

package com.solution.repo;

import com.solution.domain.A;
import com.solution.domain.B;
import com.solution.domain.C;
import com.solution.domain.Domain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

    @Component
    public class DomainRepository {
    
        @Autowired
        ARepository aRepository;
        @Autowired
        BRepository bRepository;
        @Autowired
        CRepository cRepository;
    
        public List<Domain> findDomains(String email) {
            List<Domain> result = new ArrayList<>();
            List<A> aList;
            List<B> bList;
            List<C> cList;
            aList = aRepository.findByEmailAddress(email);
            if (Objects.nonNull(aList))
                result.addAll(aList);
            bList = bRepository.findByEmailAddress(email);
            if (Objects.nonNull(bList))
                result.addAll(bList);
            cList = cRepository.findByEmailAddress(email);
            if (Objects.nonNull(cList))
                result.addAll(cList);
            return result;
    
        }
    }

A sample on how you can operate the code with CommandLineRunner spring example

package com.solution;

import com.solution.domain.A;
import com.solution.domain.B;
import com.solution.domain.C;
import com.solution.domain.Domain;
import com.solution.repo.DomainRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.List;


@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {


    private static Logger LOG = LoggerFactory
            .getLogger(SpringBootConsoleApplication.class);



    public static void main(String[] args) {
        LOG.info("STARTING THE APPLICATION");
        SpringApplication.run(SpringBootConsoleApplication.class, args);
        LOG.info("APPLICATION FINISHED");
        LOG.info("EXECUTING : command line runner");

    }

    @Bean
    public DomainRepository getDomainRepository(){
        return new DomainRepository();

    }
    @Override
    public void run(String... args) {
        LOG.info("EXECUTING : command line runner");
        List<Domain> result=getDomainRepository().findDomains("raj@com");
        for(Domain entity: result){
            if(entity instanceof A){
                System.out.println(((A) entity).getId());
                //Do your operations here.
            }
            else if(entity instanceof B){
                System.out.println(((B) entity).getID());
                //Do your operations here.
            }
            else if(entity instanceof C){
                System.out.println(((C) entity).getID());
                //Do your operations here.
            }
        }
    }
}
  •  Tags:  
  • Related