嘿,我想创建一个库,扩展JpaRepository和fetch结果,而不编写实际的查询。在我的示例中,我有两个表、图书和作者,由许多人映射到多个关系中,假设我想通过一个特定的author_id获取书籍列表,因为在我的图书实体中,我没有任何名为author_id的字段,所以我将如何使用JPARepository来获取结果而无需编写实际的查询。我所做的事情如下:我创建了一个包含图书和作者对象的bookDTO,我创建了bookDTORepository扩展JpaRepository并调用了List<Book> findByAuthor_Id(Integer id);,但是它的抛出错误是:Not an managed type: class golive.data.bookdto我的Book类是
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;
}
}
我的作家课是
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;
}
}
我的书课是
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);
}
我的书控制器方法,我补充道:
@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);
}
发布于 2015-06-18 10:30:49
您希望找到特定作者的所有书籍,因此,给定作者,请检索其作者集包含指定作者的所有图书。
相关的JPQL运营商是:
_
非运算符的NOT成员检查指定元素是否包含在指定的持久性集合字段中。 例如: 如果语言包含'English‘,c.languages的'English’成员是正确的,如果不是,则为FALSE。如果语言不包含'English‘,'English’不是c.languages的成员。
正如您可能(或不知道)所知道的,您正在使用Spring数据,它可以根据方法名称为您派生一些查询。然而,docs没有提到对非操作符成员的支持:
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);
}作为参数传递的地方是从数据库(通过您的AuthorRepository)检索的持久实例。
https://stackoverflow.com/questions/30910420
复制相似问题