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;
    }


}

助けてください

4

2 に答える 2