我有一個用例,我需要根據 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;
}
現在假設我有電子郵件地址:[email protected]
我的用例是從每個表中獲取與此電子郵件地址關聯的記錄并對主鍵進行一些處理。
就像是
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.
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/419605.html
標籤:
