自定义查询,用于从Spring Data Jpa中的多个表中获取数据
实体正在追踪
@Entitypublic class Product implements Serializable {
/*@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@NotNull(message = "Product name must not be null")
@NotEmpty
private String name;
@ManyToOne
@JoinColumn(name="category_id")
private Category category;
@ManyToMany(mappedBy = "productlist")
private List<OrderDetail> orderDetail =new ArrayList<OrderDetail>();
//getters setter
@Entitypublic class OrderDetail {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@ManyToOne
@JoinColumn(name="purchased_By")
private user PurchasedBy;
@ManyToMany
private Set<Product> productlist = new HashSet<Product>();
这些实体生成的表名为“ order_detail_productlist”,其字段如下order_detail_id和productlist_id
我正在mysql编辑器中运行以下查询,并且正在运行
select u.id, r.name from order_detail u inner join order_detail_productlist ur on(u.id=ur.order_detail_id) inner join product r on(ur.productlist_id=r.id) where u.id="?"
但是当我在带有@Query注释的spring存储库中运行时,这给了我异常。我尝试根据实体将Order_detail的名称更改为OrderDetail,但在两种情况下都相同。
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [select r.name from com.example.Domain.OrderDetail u inner join order_detail_productlist ur on(u.id=ur.order_detail_id) inner join Product r on(ur.productlist_id=r.id) where u.id= :id ]
我想要的是 。我正在尝试以这种方式使用。
public final static String product_ordered ="select r.name from OrderDetail u inner join order_detail_productlist ur " + "on(u.id=ur.order_detail_id) inner join Product r" +
" on(ur.productlist_id=r.id)" +
" where u.id= :id ";
@Query(product_ordered)
public List<Product> findById(@Param("id") int id);
我想从多个表中获取数据,例如订单等产品。
回答:
您的查询不是hibernate有效的有效HQL查询。您可以使用本机SQL查询,但是使用HQL可以轻松实现上述用例。在此之前,让我们为ManytoMany关联使用正确的注释映射:
@Entity@Table(name = "order_detail")
public class OrderDetail {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@ManyToOne
@JoinColumn(name="purchased_By")
private user PurchasedBy;
@ManyToMany
@JoinTable(
name="order_detail_productlist",
joinColumns=@JoinColumn(name="order_detail_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="productlist_id", referencedColumnName="id"))
private Set<Product> productlist = new HashSet<Product>();
产品:
@Entity@Table(name ="product")
public class Product implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@NotNull(message = "Product name must not be null")
@NotEmpty
@Column(name = "name", nullable = false)
private String name;
@ManyToOne
@JoinColumn(name="category_id")
private Category category;
@ManyToMany(mappedBy = "productlist")
private List<OrderDetail> orderDetail =new ArrayList<OrderDetail>();
并查询:
public final static String product_ordered ="Select p from Product p Join p.orderDetail od Where od.id = :id";@Query(product_ordered)
public List<Product> findById(@Param("id") int id);
这是从JPA开始的初学者友好资源
以上是 自定义查询,用于从Spring Data Jpa中的多个表中获取数据 的全部内容, 来源链接: utcz.com/qa/421976.html