如何编写hibernate条件查询,其中包括rowcount的比较

时间:2014-12-05 11:02:23

标签: java mysql sql hibernate hibernate-criteria

我需要一些关于如何为下面的SQL查询编写hibernate条件查询的建议,如下所述。

SELECT eout.EMPLOYEE_ID,eout.FIRST_NAME,COUNT(oout.ORDER_ID)
FROM EMPLOYEES eout
        JOIN
    ORDERS oout
        ON eout.EMPLOYEE_ID = oout.EMPLOYEE_ID
    GROUP BY EMPLOYEES.EMPLOYEE_ID
        HAVING COUNT(ORDERS.ORDER_ID)>5;

员工和订单的实体类如下

@Entity
@Table(name="orders")
public class Orders {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="order_id")
    int orderId;

    @OneToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="customer_id")
    Customers customers;

    @OneToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="employee_id")
    Employees employees;

    @Column(name="order_date")
    Date orderDate;

    @Column(name="required_date")
    Date requiredDate;

    @Column(name="shipped_date")
    Date shippedDate;

    @OneToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="ship_via")
    Shippers shippers;

    @Column(name="freight")
    float freight;

    @Column(name="ship_name")
    String shipName;

    @Column(name="ship_address")
    String shipAddress;

    @Column(name="ship_city")
    String shipCity;

    @Column(name="ship_region")
    String shipRegion;

    @Column(name="postal_code")
    String postalCode;

    @Column(name="ship_country")
    String shipCountry;


  //getters and setters;
}

员工实体。

@Entity
@Table(name="employees")
public class Employees {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="employee_id")
    int employeeId;

    @Column(name="last_name")
    String lastName;

    @Column(name="first_name")
    String firstName;

    @Column(name="title")
    String title;

    @Column(name="titleOfCourtsey")
    String titleOfCourtsey;

    @Column(name="birthDate")
    Date birthDate;

    @Column(name="hireDate")
    Date hireDate;

    @Column(name="address")
    String address;

    @Column(name="city")
    String city;

    @Column(name="region")
    String region;

    @Column(name="postalcode")
    String postalCode;

    @Column(name="country")
    String country;

    @Column(name="home_phone")
    int homePhone;

    @Column(name="extension")
    String extension;

    @Column(name="notes")
    String notes;

     @ManyToOne
     @JoinColumn(name="reports_to")
     Employees employees;


    //getters and setters
}

如何为顶部写的SQL查询编写Hibernate Criteria查询。

0 个答案:

没有答案