英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论