用于从spring Data Jpa中的多个表中获取数据的自定义查询

时间:2017-03-20 16:01:47

标签: hibernate spring-data-jpa jpql

实体正在关注

产品表

@Entity
public 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

订单详情表

@Entity
public 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>();
  

这些实体生成名为&#39; order_detail_productlist&#39;   和字段如下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);

我想从多个表中获取数据,例如orderes等产品。

2 个答案:

答案 0 :(得分:3)

您的查询不是一个有效的HQL查询,这是Hiberate理解的。您可以使用本机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);

Here是一个初学友好的资源,以JPA

开头

答案 1 :(得分:0)

您的工作方式是不可能的,因为您将结果集分配给List<Product>,但查询有一些连接,这意味着查询的结果不是产品实体

您可以尝试执行本机查询。例如:

@PersistenceContext
private EntityManager entityManager;

public List<Object[]> customQuery(int id) {
    Query nativeQuery = entityManager.createNativeQuery(product_ordered).setParameter("id",id);
    return nativeQuery.getResultList();
}