Hibernate查询

时间:2012-06-06 11:52:31

标签: java hibernate orm

对不起我的愚蠢问题。我有产品和订单表(有很多关系),我也有一个用户表。现在我想通过user_id和特殊字段" order_satus"来获取产品数量。我可以通过特殊标准让两个查询获得订单,然后按顺序获得产品的大小。但这根本不是最优的。当我使用JDBCTemplate时,我做了很多连接,只得到一个查询。

以下是我的实体:

@Entity
@Table(name = "shop.order")
public class Order {
@Id
@Column(name = "order_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long orderId;

private long user_id;

@Column(name = "databegin")
private Date dateBegin;

@Column(name = "dataend")
private Date dateEnd;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", insertable = false, updatable = false)
private User user;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "order_product", joinColumns = { @JoinColumn(name = "order_id") }, inverseJoinColumns = { @JoinColumn(name = "product_id") })
private List<Product> products;
}

产品实体

 @Entity
    @Table(name = "product")
    public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int product_id;


    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "order_product", joinColumns = { @JoinColumn(name = "product_id") }, inverseJoinColumns = { @JoinColumn(name = "order_id") })
    private List<Order> order;

    public List<Order> getOrder() {
        return order;
    }

    public void setOrder(List<Order> order) {
        this.order = order;
    }

    @Column
    @NotBlank
    private String name;

    @Column
    @Max(value = 250)
    private String descr;

    @Column
    @Max(value = 250)
    private String manufacturer;

    @Column
    private Double price;

    @Column
    private Byte[] barcode;

    @Column
    private Byte[] picture;

    @ForeignKey(name = "category_id")
    private int category_id;

3 个答案:

答案 0 :(得分:2)

正如你给sql查询..

  

'从ord.id =?的produtc p join order ord中选择计数(*)和   ord.status =?' - Igor Masternoy

根据您提供的实体结构,HQL将是..

select ord.products productList from Order ord where ord.id=? and ord.status=?

此查询将返回产品列表(List<Product> products),然后您可以通过java代码获取计数,即productList.size();此大小是您根据订单ID和订单状态所需的产品数量将作为参数传递,您也可以将user.id附加到根据用户过滤结果的位置。

根据您的需要,这是productList ..

Query query = getSession().createQuery("select ord.products productList from Order ord where ord.id=:orderID and ord.status=:orderStatus");
query.setInteger("orderID", orderIDParameter);
query.setString("orderStatus", orderStatusParameter);
List<Product> productList = (List<Product>) query.list();
Integer productCount = productList.size();

此productCount是您需要的产品数量。

答案 1 :(得分:0)

如果我做得对,你可以为一个用户订购许多订单,为一个订单订购许多产品。

我认为一个好的选择是使用DetachedCriteria并使用它构建查询。

看起来像(未经测试):

DetachedCriteria userCriteria = DetachedCriteria.forClass(User.class);
userCriteria.add(Projections.groupProperty("user_id"));
userCriteria.add(Projections.count("product_id");
DetachedCriteria orderCriteria = userCriteria.createCriteria("order.user_id","order",CriteriaSpecification.LEFT_JOIN);
DetachedCriteria orderCriteria = orderCriteria.createCriteria("order_product.order_id","product",CriteriaSpecification.LEFT_JOIN);
//orderCriteria.add(Restrictions.eq(...); // I can't see a "status" field in your model

List results = userCriteria.list();

答案 2 :(得分:0)

select size(ord.products) from Order ord where ord.id = :orderId and ord.status = :orderStatus

select count(*) from Order ord join ord.products where ord.id = :orderId and ord.status = :orderStatus

第一个表单将生成子查询以获取计数。第二个依赖于join来创建应用计数的sql产品。在我看来,第一种形式更直观,而第二种形式在大多数情况下表现更好。