我有一些 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帮助