建立条件查询以包含对象

时间:2019-03-20 06:15:19

标签: java sql hibernate

我有一个表实体,如下所示:

@Entity
@Table(name = "RMS_TABLES", schema = "PUBLIC", catalog = "RMS")
public class TablesEntity {
    private long id;
    private String name;
    private Timestamp createdTimestamp;
    private Timestamp updatedTimestamp;
}

类似地,我有一个订单实体,如下所示:

@Entity
@Table(name = "RMS_ORDERS", schema = "PUBLIC", catalog = "RMS")
public class OrdersEntity {
    private long id;
    private CustomersEntity customer;
    private String status;
    private Timestamp createdTimestamp;
    private Timestamp updatedTimestamp;
    private TablesEntity table;
    private List<OrderDetailEntity> orders;
    private double subTotal;
    private double discount;
    private double vat;
    private double serviceCharge;
    private double total;
}

我要做的是根据ID和表名列出顺序。

到目前为止,我已经在订单存储库中执行了以下操作:

Session session = null;
        try {
            // Get the record with the username.
            session = HibernateUtility.getSessionFactory().openSession();
            session.beginTransaction();
            CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
            CriteriaQuery<OrdersEntity> criteriaQuery = criteriaBuilder.createQuery(OrdersEntity.class);
            Root<OrdersEntity> root = criteriaQuery.from(OrdersEntity.class);

            Predicate[] predicates = new Predicate[1];
            predicates[0] = criteriaBuilder.equal(root.get("id"), searchText);

            criteriaQuery.select(root).where(predicates);
            TypedQuery<OrdersEntity> allQuery = session.createQuery(criteriaQuery);
            List<OrdersEntity> ordersEntity = allQuery.getResultList();
            return ordersEntity;
        } catch (HibernateException e){
            e.printStackTrace();
            return null;
        } finally {
            if ( session != null) session.close();
        }

0 个答案:

没有答案