JpaRepository 및 중첩된 개체 목록으로 검색하는 방법은 무엇입니까?
묘사
있습니다.PersonRepository
그리고.Person
독립체,Person
클래스에 포함됨List<Qualification>
.Qualification
클래스에는 3개의 간단한 필드가 있습니다.
추가하려고 했습니다.@Query
사용자 정의 방법에 대한 주석 및 JPQL을 사용하여 결과를 얻지만Qualification
클래스 필드는 저장소 자체에 포함되어 있기 때문에 JPQL에서 조작에 사용할 수 없습니다.List<Qualification>
단순한 분야가 아닌Qualification
.
자격 증명의 중첩 필드로 검색하려면 어떻게 해야 합니까?
쿼리
이제 자격증의 경력InMonths가 3보다 크고 9보다 작은 사람과 자격증의 이름 필드 = 'filename'인 사람의 목록을 찾아야 합니다.
코드
인물.자바
@Data
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
@NotEmpty
@Size(min = 2)
private String name;
@NotEmpty
@Size(min = 2)
private String surname;
@ElementCollection(targetClass = java.util.ArrayList.class, fetch = FetchType.EAGER)
private List<Qualification> qualifications = new ArrayList<>();
}
사용자 리포지토리.java
@Repository
public interface PersonRepository extends JpaRepository<Person, String> {
}
자격.java
@Data
@AllArgsConstructor
public class Qualification implements Serializable {
@Id @GeneratedValue
private String id;
private String name;
private String experienceInMonths;
}
편집: 중첩된 개체의 컬렉션이므로 이 게시물을 복제할 수 없습니다.단일 참조가 아닙니다.
첫째, 변경experienceInMonths
부터String
로.int
(문자열을 숫자와 비교할 수 없는 경우).그런 다음 이 '소시지'를 사용해 볼 수 있습니다.
List<Person> findByQualifications_experienceInMonthsGreaterThanAndQualifications_experienceInMonthsLessThanAndName(int experienceGreater, int experienceLess, String name);
또는 다음과 같은 매우 좋은 방법을 사용할 수 있습니다.
@Query("select p from Person p left join p.qualifications q where q.experienceInMonths > ?1 and q.experienceInMonths < ?2 and q.name = ?3")
List<Person> findByQualification(int experienceGreater, int experienceLess, String name);
메서드 이름 안에 '_'(언더스코어) 문자를 사용하여 JPA가 분할할 위치를 정의할 수 있습니다.
이 경우 우리의 메소드 이름은
List<Person> findByQualification_name(String name);
언급URL : https://stackoverflow.com/questions/47996810/how-to-search-with-jparepository-and-nested-list-of-objects
'sourcecode' 카테고리의 다른 글
측면 항목의 너비가 다를 때 중간 항목 중심을 유지합니다. (0) | 2023.09.06 |
---|---|
CSS에서 *와 *|*의 차이점은 무엇입니까? (0) | 2023.09.01 |
포스트백 시 확인 요약에 오류 메시지를 추가하려면 어떻게 해야 합니까? (0) | 2023.09.01 |
IE9은 F12를 친 후에만 아약스 호출을 올바르게 수행합니다. (0) | 2023.09.01 |
MySQL: 필드 기본값을 다른 열로 설정 (0) | 2023.09.01 |