예제 #1
0
  @Test
  public void testPrefixMacro() {
    CriteriaBuilder<Document> cb = cbf.create(em, Document.class, "d");
    cb.where("PREFIX(d, id)").eq(1L);
    String expected = "SELECT d FROM Document d WHERE d.id = :param_0";

    assertEquals(expected, cb.getQueryString());
    cb.getResultList();
  }
예제 #2
0
  @Test
  public void testScopedMacro() {
    CriteriaBuilder<Document> cb = cbf.create(em, Document.class, "d");
    CatchException.verifyException(cb, SyntaxErrorException.class).where("ID(d)");

    cb.registerMacro(
        "id",
        new JpqlMacro() {
          @Override
          public void render(FunctionRenderContext context) {
            context.addArgument(0);
            context.addChunk(".id");
          }
        });
    cb.where("ID(d)").eq(1);
    String expected = "SELECT d FROM Document d WHERE d.id = :param_0";
    assertEquals(expected, cb.getQueryString());

    CriteriaBuilder<Document> newCb = cbf.create(em, Document.class, "d");
    CatchException.verifyException(newCb, SyntaxErrorException.class).where("ID(d)");
  }
  @Test
  public void testSelectNewDocumentObjectBuilder() {
    CriteriaBuilder<String[]> criteria =
        cbf.create(em, Document.class, "d")
            .selectNew(
                new ObjectBuilder<String[]>() {

                  @Override
                  public <X extends SelectBuilder<X>> void applySelects(X queryBuilder) {
                    queryBuilder
                        .select("name", "name")
                        .select("UPPER(name)", "upperName")
                        .select("LOWER(name)", "lowerName")
                        .select("false");
                  }

                  @Override
                  public String[] build(Object[] tuple) {
                    return new String[] {(String) tuple[0], (String) tuple[1], (String) tuple[2]};
                  }

                  @Override
                  public List<String[]> buildList(List<String[]> list) {
                    return list;
                  }
                });
    assertEquals(
        "SELECT d.name AS name, UPPER(d.name) AS upperName, LOWER(d.name) AS lowerName, "
            + STATIC_JPA_PROVIDER.getBooleanExpression(false)
            + " FROM Document d",
        criteria.getQueryString());
    List<String[]> actual = criteria.getQuery().getResultList();

    assertArrayEquals(new String[] {"Doc1", "DOC1", "doc1"}, actual.get(0));
    assertArrayEquals(new String[] {"Doc2", "DOC2", "doc2"}, actual.get(1));
  }