您的查询不是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开始的初学者友好资源
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)