拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 从具有不同型别主键的多个表中获取记录。休眠

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

白鹭 - 2022-02-17 2082 0 0

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

@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

uj5u.com热心网友回复:

您可以在需要时使用 Java 型别转换来执行特定操作。首先,您将所有三个物体类放在一个保护伞中。然后创建同一个父类的子类。

package com.solution.domain;

public class Domain {
}

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


}

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

物体类 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   '\''  
                '}';
    }
}

访问所有三个 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.
            }
        }
    }
}
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *