如何使用 Spring 数据获取数据

How to Fetch Data using Spring Data

嘿,我想创建一个扩展 JpaRepository 的存储库并在不编写实际查询的情况下获取结果, 在我的示例中,我有 2 个表 Book 和 Author 由多对多关系映射,假设我想通过特定 author_id 获取图书列表,因为在我的图书实体中,我没有任何名为 [= 的字段56=],那么我将如何在不编写实际查询的情况下使用 JPARepository 获取结果。 我正在做这样的事情:我创建了一个包含 Book 和 Author 对象的 bookDTO,我创建了扩展 JpaRepository 的 bookDTORepository 并正在调用 List<Book> findByAuthor_Id(Integer id); ,但它的抛出错误为:Not an managed type: class golive.data.bookdto My book class 是

package golive.data;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sun.istack.internal.NotNull;

@Entity
@Table(name="book")
public class Book implements java.io.Serializable{

 @Id
 @GeneratedValue
 private Integer id;
 
 @NotNull
 @Column(name="name")
 private String name;


 @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
 @JoinTable(name = "writes", joinColumns = { @JoinColumn(name = "book_id") }, inverseJoinColumns = { @JoinColumn(name = "author_id") })
 private Set<Author> authors = new HashSet<Author>(); 


 public Set<Author> getAuthors() {
  return authors;
 }

 public Integer getId() {
  return id;
 }

 public String getName() {
  return name;
 }

 public void setAuthors(Set<Author> authors) {
  this.authors = authors;
 }

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

 public void setName(String name) {
  this.name = name;
 }
 
 
}

我的作者 class 是

package golive.data;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sun.istack.internal.NotNull;

@Entity
@Table(name="author")
public class Author implements java.io.Serializable{

 @Id
 @GeneratedValue
 @Column(name="id")
 private Integer Id;
 
 @NotNull
 @Column(name="name")
 private String name;


 public Integer getId() {
  return Id;
 }

 public String getName() {
  return name;
 }



 public void setId(Integer id) {
  Id = id;
 }

 public void setName(String name) {
  this.name = name;
 }
 
}

我的 bookdto class 是

package golive.data;

public class bookdto {

 private Book book;
 private Author author;
 public Book getBook() {
  return book;
 }
 public void setBook(Book book) {
  this.book = book;
 }
 public Author getAuthor() {
  return author;
 }
 public void setAuthor(Author author) {
  this.author = author;
 }
 
}
我的 bookDTORepository 是:

package golive.data;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

public interface bookDTORepository extends JpaRepository<bookdto, Book> {
 
 List<Book> findByAuthor_Id(Integer id);
 
}

我的book controller方法,我补充说:

 @RequestMapping(value = "/listbyauthor", method = RequestMethod.POST, produces = "application/json")
 public ResponseEntity<List<Book>> getBookByAuthorId(@RequestBody Author author,HttpServletResponse response) {
  try {
   Author temp = new Author();
   temp.setId(author.getId());
   temp.setName(author.getName());
   return new ResponseEntity<>(bookRepository.findByAuthor(temp), HttpStatus.OK);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return new ResponseEntity<>(HttpStatus.NO_CONTENT);
 }

您想查找特定作者的所有书籍,因此,给定一个作者,检索其作者集包含指定作者的所有书籍。

相关的 JPQL 运算符是:

http://www.objectdb.com/java/jpa/query/jpql/collection#NOT_MEMBER_OF_

[NOT] MEMBER [OF] The [NOT] MEMBER OF operator checks if a specified element is contained in a specified persistent collection field.

For example:

'English' MEMBER OF c.languages is TRUE if languages contains 'English' and FALSE if not. 'English' NOT MEMBER OF c.languages is TRUE if languages does not contain 'English'.

您可能(或可能不知道)知道,您正在使用 Spring 数据,它可以根据方法名称为您派生出一些查询。然而,文档没有提到对 [NOT] MEMBER [OF] 运算符的支持:

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation

因此,您需要向您的存储库添加一个自定义查询方法,该方法类似于:

public interface BookRepository extends JpaRepository<Book, Integer> {

    @Query("select b from Book b where ?1 MEMBER OF b.authors")
    List<Book> findByAuthor(Author author);
}

其中 作为参数传递的 Author 是从数据库(通过您的 AuthorRepository)检索的持久实例。