如何使用Spring数据获取数据

时间:2015-06-18 08:39:04

标签: spring jpa spring-boot spring-data

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

&#13;
&#13;
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;
	}
	
	
}
&#13;
&#13;
&#13;

我的作者班是

&#13;
&#13;
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;
	}
	
}
&#13;
&#13;
&#13;

我的bookdto课程是

&#13;
&#13;
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;
	}
	
}
&#13;
&#13;
&#13; 我的bookDTORepository是:

&#13;
&#13;
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);
	
}
&#13;
&#13;
&#13;

我的书籍控制器方法,我补充道:

&#13;
&#13;
	@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);
	}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您希望查找特定作者的所有图书,以便在给定作者的情况下,检索其作者集包含指定作者的所有图书。

相关的JPQL运算符是:

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

  

[NOT]会员[OF] [NOT]会员操作员检查是否指定   element包含在指定的持久集合字段中。

     

例如:

     

&#39;英语&#39;如果语言包含,则c.languages成员为TRUE   &#39;英语&#39;如果没有,则为FALSE。 &#39;英语&#39;不是c.languages的成员   如果语言不包含&#39;英语&#39;

,则为TRUE

正如您可能(或可能没有)意识到的那样,您正在使用Spring Data,它可以根据方法名称为您派生一些查询。然而,文档并未提及对[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);
}

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