Person
实体的My Spring Boot实体无效。我与Hobby
实体有多对多关系,并且该类正在运行。
不必在JpaRepository
中定义的预定义函数可以正常运行,但不能运行任何其他函数。
班级Person
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "Person")
public class Person implements Serializable {
private static final long serialVersionUID = 4582624077512606651L;
@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<>();
@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;
}
}
班级Hobby
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
@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;
}
}
班级HobbyRepository
有工作方法
import ...Hobby;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Set;
public interface HobbyRepository extends JpaRepository<Hobby, Long> {
public List<Hobby> findByPersons_Id(Long id);
public List<Hobby> findByPersons_IdIn(Set<Long> ids);
@Query("select h from Hobby h")
List<Hobby> findByHobbyHobbyId();
}
但是课程PersonRepository
的方法不起作用
import ...Person;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Set;
public interface PersonRepository extends JpaRepository<Person, Long> {
List<Person> findByLastName(String lastName);
List<Person> findByHobby_Id(Long id);
List<Person> findByHobby_IdIn(Set<Long> ids);
}
这是其中一项有效的电话。与personService.findByHobby相同的调用不起作用
String result = "";
Set <Long>ids=new HashSet<Long>();
ids.add(new Long(87));
ids.add(new Long(90));
for(Hobby cust : hobbyService.findByPerson_IdIn(ids)){
result += cust.toString();
}
return result;
我已尝试使用各种形式的JsonBackReference
进行递归数据库但不起作用。如何解决?
更新
这是一些consoleoutput。第一行是personService.findAll()
。
Hibernate: select person0_.id as id1_1_, person0_.first_name as
first_na2_1_, person0_.last_name as last_nam3_1_ from person person0_
conseleoutput personService.findByLastName(String name)
上的第二行不起作用。
Hibernate: select person0_.id as id1_1_, person0_.first_name as
first_na2_1_, person0_.last_name as last_nam3_1_ from person person0_ where person0_.last_name=?
表爱好
id(PK) title
'88', 'Fotboll',
'89', 'Film',
'91', 'Frimärken',
'92', 'Golf',
表人
id(PK) first_name last_name
'87', 'Adam', 'Johnson'
'90', 'Anna', 'Persson'
'93', 'Nils', 'Svensson'
表person_hobby
person_id(pk) hobby_id(pk)
'87', '88'
'87', '89'
'90', '89'
'90', '91'
'90', '92'
'93', '92'
答案 0 :(得分:0)
查看您的班级PersonServiceImpl
第44行。您会在那里找到包含该行findByHobby_Id
的相同方法的调用。可能通过该类实现的接口。
修复该行以调用您实际想要调用的内容(我猜您的存储库?)。