gpt4 book ai didi

java - 为什么在通过连接 JPA 中的两个不相关实体来使用投影获取数据时未设置投影字段?

转载 作者:行者123 更新时间:2023-12-02 09:29:39 24 4
gpt4 key购买 nike

我试图通过连接两个不相关的实体来使用投影来获取一些特定的列值。但我没有得到任何值(value)观。存储库代码是:-


@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {
@Query("SELECT p.id,p.firstName,p.lastName,a.city FROM Person p INNER JOIN Address a on p.id = a.personId")
public List<PersonAndAddressSummary> findAllPersonSummary();
}

人员类别

@Entity
public class Person {
@Id
private long id;
private String firstName;
private String lastName;
private long phone;

public long getId() {
return id;
}

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

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public long getPhone() {
return phone;
}

public void setPhone(long phone) {
this.phone = phone;
}
}
Address class

@Entity
public class Address {
@Id
private int id;
private String street;
private String state;
private int personId;
private String country;
private int houseNo;
private String city;

public int getId() {
return id;
}

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

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public int getPersonId() {
return personId;
}

public void setPersonId(int personId) {
this.personId = personId;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public int getHouseNo() {
return houseNo;
}

public void setHouseNo(int houseNo) {
this.houseNo = houseNo;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}
}

投影界面

public interface PersonAndAddressSummary {
public String getId();

public String getFirstName();

public String getLastName();

public String getCity();
}

执行此 Spring Boot 应用程序后,我得到的输出是这样的。

[{"lastName":null,"firstName":null,"city":null,"id":null},{"lastName":null,"firstName":null,"city":null,"id":null},{"lastName":null,"firstName":null,"city":null,"id":null}]

尽管存在与地址类中的每个人员完美匹配的可用记录(人员和地址表中可用的数据)。

最佳答案

如果两个实体之间没有连接,那么您无法获取其他实体的数据。每个存储库都是 jpa 中特定于类的。您必须为每个类别定义不同的投影。

@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {
@Query("SELECT p FROM Person p")
public List<PersonAndSummary> findAllPersonSummary();
}


@Repository
public interface AddressRepository extends CrudRepository<Address, Long> {
@Query("SELECT a FROM Address a where a.personId=:personId")
public AddressSummary findAddressSummary(@Param("personId") int personId);
}



public interface PersonSummary {
public String getId();
public String getFirstName();
public String getLastName();
}


public interface AddressSummary {

public String getCity();
}


public interface PersonAndAddressSummary extends PersonSummary, AddressSummary{
}

现在在您的服务类中找到 personSummary 列表。现在迭代此列表并通过 personId 获取 AddressSummary。

List<PersonSummary> personSummaryList = yourRepositoryName.findAllPersonSummary();
List<PersonAndAddressSummary> list = new ArrayList<>();
for(PersonSummary summary: personSummaryList){
AddressSummary addressSummary = repositoryName.findAddressSummary(summary.getId());
//now add required fields in your list
}

关于java - 为什么在通过连接 JPA 中的两个不相关实体来使用投影获取数据时未设置投影字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58090461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com