gpt4 book ai didi

java - 查询返回大量重复记录

转载 作者:行者123 更新时间:2023-12-02 02:26:59 25 4
gpt4 key购买 nike

我尝试从两个表(Statement、AppCurContract)进行查询,但收到很多重复记录。即使我仅从 Statement 进行查询,我也会收到相同的结果。当我将 appCurContracts 字段添加到 Statement bean 时,它就开始了。

我在这里发现了同样的问题 Spring Data JPA query return repeated row instead of actual data, why?

但是我在两个表中都有唯一的键。我做错了什么?

这是我的代码

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Entity
@Table
@Data
@EqualsAndHashCode(exclude = "appCurContracts")
public class Statement {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String numStatement;
@Column(updatable = false)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime dateTimeSubmStatement;
private int filialId;
private int myself;
private int status;
private Date modifyDate;
private String nameNonResident;
private String email;
private Integer typeStatement;
@OneToMany(mappedBy = "statement", cascade = CascadeType.ALL)
private Set<AppCurContract> appCurContracts;
public Statement() {
super();
}

public Statement(String nameDocument, String numStatement, LocalDateTime dateTimeSubmStatement, String jurPerson, String iin_bin, int filialId, int myself, int status, Date modifyDate, String nameNonResident, String contractNum, Date contractDate, String phone, String email, Integer typeStatement, String json, String iinBinRight, AppCurContract... appCurContracts) {
this.numStatement = numStatement;
this.dateTimeSubmStatement = dateTimeSubmStatement;
this.filialId = filialId;
this.myself = myself;
this.status = status;
this.modifyDate = modifyDate;
this.nameNonResident = nameNonResident;
this.email = email;
this.typeStatement = typeStatement;
this.appCurContracts = Stream.of(appCurContracts).collect(Collectors.toSet());
this.appCurContracts.forEach(x -> x.setStatement(this));
}

public void setAppCurContracts(Set<AppCurContract> appCurContracts) {

for (AppCurContract child : appCurContracts) {

child.setStatement(this);
}
this.appCurContracts = appCurContracts;
}
}
import lombok.Data;
import javax.persistence.*;

@Entity
@Data
public class AppCurContract {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn()
private Statement statement;
private String jurPerson;
private String iin_bin;
private String nameDocument;
private String contractNum;
private String contractDate;

public AppCurContract() {
super();
}

public AppCurContract(String jurPerson, String iin_bin, String nameDocument, String contractNum, String contractDate) {
this.jurPerson = jurPerson;
this.iin_bin = iin_bin;
this.nameDocument = nameDocument;
this.contractNum = contractNum;
this.contractDate = contractDate;
}
}


public interface StatementRepo extends JpaRepository<Statement, Long> {

@Query("SELECT d FROM Statement d JOIN d.appCurContracts e" +
" WHERE d.status = ?1")

// @Query("SELECT d FROM Statement d WHERE d.status = ?1")

List<Statement> findByStatus(Integer status);

List<Statement> findStatementsByEmailEquals(String email);
}

编辑仔细查看 JSON 结果,我发现结果不仅是重复的,而且字段“appCurontract”包含嵌套语句,然后又包含“appCurContract”(相互嵌套)等。我认为是无限期的。我预计只有 5 条记录。

最佳答案

自己定义哈希码方法,任何必要条件如 set uses this to check for duplicates 。您的代码(在存储库实现中)将变为:

List<Statement> findDistinctByEmail(String email);

并且您不应该需要查询注释。

关于java - 查询返回大量重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57247263/

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