0

我有一些 Spring Boot 问题。我使用 PostgreSql 并希望通过提供 3 个爱好参数来查找 Person。我有一个带有 1 个参数的工作示例....

public interface SubjectRepository extends JpaRepository<Subject, Long> {
    Collection<Person> findByPersonsHobbyId(String hobbyId1);
}

...但我想知道是否有一些类似的解决方案?

public interface SubjectRepository extends JpaRepository<Subject, Long> {
    Collection<Person> findByPersonsHobbyId(Set<String> hobbyIds);
}

或者喜欢这个...

public interface SubjectRepository extends JpaRepository<Subject, Long> {
    Collection<Person> findByPersonsHobbyId(String hobbyId1, String hobbyId2, 
        String hobbyId3);
}

更新

爱好。

package com.proj.db_proj;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;

import net.minidev.json.annotate.JsonIgnore;


@Entity
@Table(name="Hobby")
public class Hobby implements Serializable {

    private static final long serialVersionUID = 1442665206056041894L;

    @ManyToMany(fetch = FetchType.LAZY,
        cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
        },
        mappedBy = "hobby")
    private Set<Person> persons = new HashSet<>();

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @Column(name="title")
    private String title;

    protected Hobby() {
    }

    public Hobby(String title) {
        this.title = title;

    }

    @Override
    public String toString() {
        return "Hobby [persons=" + persons + ", id=" + id + ", title=" + 
        title + "]";
    }

    public Set<Person> getPersons() {
        return persons;
    }

    public void setPersons(Set<Person> persons) {
        this.persons = persons;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

和拥有人实体

package com.proj.db_proj;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;

import net.minidev.json.annotate.JsonIgnore;
@Entity
@Table(name="person")
public class Person implements Serializable {


    @ManyToMany(fetch = FetchType.LAZY,
            cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
            })
    @JoinTable(name = "person_hobby",
            joinColumns = { @JoinColumn(name = "person_id") },
            inverseJoinColumns = { @JoinColumn(name = "hobby_id") })
    private Set<Hobby> hobby = new HashSet<>();

    private static final long serialVersionUID = 4582624077512606651L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @Column(name="firstName")
    private String firstName;

    @Column(name="lastName")
    private String lastName;


    protected Person() {
    }

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format("Person[id=%d, firstName='%s', lastName='%s']", 
id, firstName, lastName);
    }

    public Set<Hobby> getHobby() {
        return hobby;
    }

    public void setHobby(Set<Hobby> hobby) {
        this.hobby = hobby;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }


}

将apprichiate帮助

4

2 回答 2

0

对于第二个变体,您可以使用以下构造

public interface SubjectRepository extends JpaRepository<Subject, Long> {

    @Query("select p from Person p where p.hobbyId in ( :hobbyId1, :hobbyId2, :hobbyId3 ")
    List<Person> findByPersonsHobbyId(@Param("hobbyId1") String hobbyId1, @Param("hobbyId2") String hobbyId2, @Param("hobbyId3") String hobbyId3);
}
于 2018-02-16T20:21:14.807 回答
0

您可以In在使用集合作为输入时使用

public interface SubjectRepository extends JpaRepository<Subject, Long> {
    Collection<Person> findByPersonsHobbyIdIn(Set<String> hobbyIds);
}

有关更多此类关键字的信息,请参阅此处

于 2018-02-15T10:56:44.143 回答