Spring JPA – 通过关联表列查找 – 已解决

huangapple go评论58阅读模式
英文:

Spring JPA - Find by join table column - SOLVED

问题

我有以下两个表:

@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DiscountContract {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    private Long id;
    private String extReferenceId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "contract_type_id", nullable = false, referencedColumnName = "id")
    private ContractType contractType;
}

@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ContractType {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    private Long id;
    private String type;
    private String description;
    private LocalDateTime createdDate;
    private String createdBy;
    private LocalDateTime lastUpdatedDate;
    private String lastUpdatedBy;
}

我想编写一个SPeL查询,根据主表(DiscountContract)中的 extReferenceId 和关联表(ContractType)中的 type 来查找记录。

我尝试了以下查询语句 findDiscountContractByExtReferenceIdAndContractTypeType("12343212", "test"),但遇到了以下问题:

方法引发了 java.lang.StackOverflowError 异常。无法评估 java.util.Optional.toString()

英文:

I have following two tables:

@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DiscountContract {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    private Long id;
    private String extReferenceId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "contract_type_id", nullable = false, referencedColumnName = "id")
    private ContractType contractType;
}

and

@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ContractType {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    private Long id;
    private String type;
    private String description;
    private LocalDateTime createdDate;
    private String createdBy;
    private LocalDateTime lastUpdatedDate;
    private String lastUpdatedBy;

}

I would like to write SPeL query to find out the records based on extReferenceId from the main (DiscountContract) table and type from the join table(ContractType) table

I tried with this query findDiscountContractByExtReferenceIdAndContractTypeType("12343212", "test") but facing this issue

> Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate java.util.Optional.toString()

答案1

得分: 1

我找到了问题。虽然它与JPA无关,但我会保留这个问题,因为它可以帮助其他人找到解决这个连接查询的方法。

问题与Lombok注解@Data和自带的toString()方法有关。toString()方法会导致两个对象之间的循环调用。

总结一下,我从实体模型中移除了@Data注解,并用@Getter和@Setter代替它。

更多详细信息可以在这篇帖子中找到 - spring-jpa-bi-directional-cannot-evaluate-tostring

英文:

I found the problem. Although it's not JPA related, I will leave the question, as it can help other people to find a way to make this join query

The problem was related to the Lombok annotation @Data and toString() that come out of the box from it. toString() method causes circular calls of both objects.

To summarize, I removed the @Data annotation from the Entity models and replace it with @Getter & @Setter

More details can be found in this post - spring-jpa-bi-directional-cannot-evaluate-tostring

huangapple
  • 本文由 发表于 2023年5月29日 21:15:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定