从具有不同类型主键的多个表中获取记录。休眠

Fetch records from Multiple tables having different type of primary key. Hibernate

我有一个用例,我需要根据电子邮件地址从不同的表中获取记录,并且这些表中的每一个都有不同类型的主键(字符串、整数等)

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

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

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

现在假设我有电子邮件地址:test@gmail.com

我的用例是从每个表中获取与此电子邮件地址关联的记录并对主键进行一些处理。

类似

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

您可以在需要时使用 Java 类型转换来执行特定操作。 首先,您将所有三个实体 classes 合并到一个保护伞中。然后制作 child class 相同的 parent class.

package com.solution.domain;

public class Domain {
}

实体ClassA

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;
    }


}

实体ClassB

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;
    }
}

实体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 访问所有三个 JPA Repos 整理结果并发送列表

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;
    
        }
    }

有关如何使用 CommandLineRunner 操作代码的示例 spring 示例

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.
            }
        }
    }
}