Spring Data JPA未按查询方法名

时间:2016-07-01 19:32:28

标签: java spring jpa

所以我有一个具有属性的类,该属性由另一个类的列表组成:

@Entity
@Table(name = "persons")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer personId;

    @Column(name = "name", nullable = false)
    private String name;

    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    @JsonBackReference
    private List<PersonValue> personValues;

// getters/setters
}

PersonValue类:

@Entity
@Table(name = "person_values")
public class PersonValue {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long personValueId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "person_id")
    @JsonManagedReference
    private Person person;

    @ManyToOne
    @JoinColumn(name = "city_id")
    private City city;

    @Column(name = "name", nullable = false)
    private String name;

    // getters/setters
}

城市课程:

@Entity
@Table(name = "cities")
public class City {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer cityId;

    @ManyToOne
    @JoinColumn(name = "state_id")
    private State state;

    @Column(name = "name", nullable = false)
    private String name;

    // getters/setters
}

通过使用查询方法名称来过滤我需要通过它的person_al的city_id检索的人员,我得到一个带有PersonValues的Set,不仅是我传给它的城市&#39; s city_id作为参数方法:

public interface PersonDAO extends JpaRepository<Person, Long> {
    Set<Person> findByPersonValuesCityCityIdOrderByStateNameAscNameAscPersonValuesNameAsc(Integer cityId);
}

所以在一个例子中,我传递了city_id 3375,它返回了一个Person的PersonValues列表,不仅有这个城市(3375),还有其他城市。

知道为什么会这样,我该如何解决?

它还在列表中返回多个重复的人,直到我将返回类型从List更改为Set。

0 个答案:

没有答案