路径预期加入错误! JPQL内连接查询有很多例子

时间:2015-06-18 10:20:39

标签: spring hibernate jpa jpql

我正在编写一个查询,其中我正在获取具有特定author_id的书籍列表,书籍和作者被映射为多个关系书实体包含id,名称和作者集以及作者实体包含名称和id。我创建了一个包含author_id和book_id的中间表写入。我没有创建任何写实体类,它在mysql db

以下查询出了什么问题,我收到了path expected join error!



@Query (SELECT b.name from Book b inner join writes w on b.id=w.book_id where w.author_id = ?1)
List<Book> findByAuthorId(Integer AuthorId);
&#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.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;

我没有任何写入实体,它在我的数据库中

1 个答案:

答案 0 :(得分:0)

您应该将nativeQuery = true添加到“查询”注释中。并将您的查询更改为本机格式。如果您的数据库名称也是Book,写入和列名称都是正确的,那么这应该有效。

@Query ("SELECT * from Book b inner join writes w on b.id=w.book_id where w.author_id = ?1", nativeQuery = true)
List<Book> findByAuthorId(Integer AuthorId);

更新

将Star添加到查询中。您不能只选择bookName来预订对象。但您可以选择书名到字符串列表。

@Query ("SELECT b.name from Book b inner join writes w on b.id=w.book_id where w.author_id = ?1", nativeQuery = true)
List<String> findByAuthorId(Integer AuthorId);