コード例 #1
0
  /** This test method demonstrates using date functions. */
  @Test
  public void testDates() {
    log.info("*** testDates() ***");

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Sale> qdef = cb.createQuery(Sale.class);
    Root<Sale> s = qdef.from(Sale.class);
    qdef.select(s);

    // select s from Sale s
    // where s.date < CURRENT_DATE
    qdef.where(cb.lessThan(s.<Date>get("date"), cb.currentDate()));
    int rows = executeQuery(qdef).size();
    assertEquals("unexpected number of rows", 2, rows);

    // select s from Sale s
    // where s.date = CURRENT_DATE
    qdef.where(cb.equal(s.<Date>get("date"), cb.currentDate()));
    rows = executeQuery(qdef).size();
    assertEquals("unexpected number of rows", 0, rows);

    // no bulk query capability in Criteria API
    rows = em.createQuery("update Sale s " + "set s.date = CURRENT_DATE").executeUpdate();
    assertEquals("unexpected number of rows", 2, rows);

    em.getTransaction().commit();
    em.clear(); // remove stale objects in cache

    // select s from Sale s
    // where s.date = CURRENT_DATE
    qdef.where(cb.equal(s.<Date>get("date"), cb.currentDate()));
    rows = executeQuery(qdef).size();
    assertEquals("unexpected number of rows", 2, rows);
  }
コード例 #2
0
  public List<SupplyOrderInformation> makeSupplyOrderBasedOnStockpile(int stockpile) {
    List<SupplyOrderInformation> result = new ArrayList<>();

    SupplyOrderHeaderManager supplyOrderHeaderManager = new SupplyOrderHeaderManager();
    SupplyOrderLineManager supplyOrderLineManager = new SupplyOrderLineManager();

    Date currentDate = new Date(System.currentTimeMillis());

    EntityManager entityManager = Utilities.getEntityManagerFactory().createEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BookPresentation> criteriaQueryBookPresentation =
        criteriaBuilder.createQuery(BookPresentation.class);
    Root<BookPresentation> rootBookPresentation =
        criteriaQueryBookPresentation.from(BookPresentation.class);
    Join<Collection, PublishingHouse> joinCollectionPublishingHouse =
        rootBookPresentation
            .join(BookPresentation_.book)
            .join(Book_.collection)
            .join(Collection_.publishingHouse);
    criteriaQueryBookPresentation.select(rootBookPresentation);
    criteriaQueryBookPresentation.where(
        criteriaBuilder.lt(rootBookPresentation.get(BookPresentation_.stockpile), stockpile));
    List<Order> orderList = new ArrayList<>();
    orderList.add(criteriaBuilder.asc(joinCollectionPublishingHouse.get(PublishingHouse_.id)));
    orderList.add(criteriaBuilder.asc(rootBookPresentation.get(BookPresentation_.id)));
    criteriaQueryBookPresentation.orderBy(orderList);
    TypedQuery<BookPresentation> typedQueryBookPresentation =
        entityManager.createQuery(criteriaQueryBookPresentation);
    List<BookPresentation> bookPresentationList = typedQueryBookPresentation.getResultList();

    PublishingHouse currentPublishingHouse = null;
    SupplyOrderHeader currentSupplyOrderHeader = null;

    for (BookPresentation bookPresentation : bookPresentationList) {
      PublishingHouse publishingHouse =
          bookPresentation.getBook().getCollection().getPublishingHouse();
      if (currentPublishingHouse == null || !currentPublishingHouse.equals(publishingHouse)) {
        SupplyOrderHeader supplyOrderHeader = new SupplyOrderHeader();
        supplyOrderHeader.setIdentificationNumber(Utilities.generateIdentificationNumber(3, 6));
        supplyOrderHeader.setIssueDate(currentDate);
        supplyOrderHeader.setState(Constants.SENT_STATUS);
        supplyOrderHeader.setPublishingHouse(publishingHouse);
        supplyOrderHeaderManager.create(supplyOrderHeader);
        currentPublishingHouse = publishingHouse;
        currentSupplyOrderHeader = supplyOrderHeader;
      }
      SupplyOrderLine supplyOrderLine = new SupplyOrderLine();
      supplyOrderLine.setBookPresentation(bookPresentation);
      supplyOrderLine.setQuantity(stockpile - bookPresentation.getStockpile());
      supplyOrderLine.setSupplyOrderHeader(currentSupplyOrderHeader);
      supplyOrderLineManager.create(supplyOrderLine);
    }
    entityManager.close();

    entityManager = Utilities.getEntityManagerFactory().createEntityManager();
    criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<SupplyOrderLine> criteriaQuerySupplyOrderLine =
        criteriaBuilder.createQuery(SupplyOrderLine.class);
    Root<SupplyOrderLine> rootSupplyOrderLine =
        criteriaQuerySupplyOrderLine.from(SupplyOrderLine.class);
    Join<SupplyOrderLine, SupplyOrderHeader> joinSupplyOrder =
        rootSupplyOrderLine.join(SupplyOrderLine_.supplyOrderHeader);
    criteriaQuerySupplyOrderLine.select(rootSupplyOrderLine);
    criteriaQuerySupplyOrderLine.where(
        criteriaBuilder.equal(
            joinSupplyOrder.get(SupplyOrderHeader_.issueDate), criteriaBuilder.currentDate()));
    criteriaQuerySupplyOrderLine.orderBy(
        criteriaBuilder.asc(joinSupplyOrder.get(SupplyOrderHeader_.id)),
        criteriaBuilder.asc(rootSupplyOrderLine.get(SupplyOrderLine_.id)));
    TypedQuery<SupplyOrderLine> typedQuerySupplyOrderLine =
        entityManager.createQuery(criteriaQuerySupplyOrderLine);
    List<SupplyOrderLine> supplyOrderLines = typedQuerySupplyOrderLine.getResultList();

    for (SupplyOrderLine supplyOrderLine : supplyOrderLines) {
      SupplyOrderInformation supplyOrderInformation = new SupplyOrderInformation();
      supplyOrderInformation.setPublishingHouseId(
          supplyOrderLine.getSupplyOrderHeader().getPublishingHouse().getId());
      supplyOrderInformation.setBookPresentationId(supplyOrderLine.getBookPresentation().getId());
      supplyOrderInformation.setQuantity(supplyOrderLine.getQuantity());
      result.add(supplyOrderInformation);
    }
    entityManager.close();

    return result;
  }