Пример #1
0
  @Test
  @RunOrder(9)
  public void queryLikeFieldTest() {

    EntityManager em = emf.createEntityManager();
    try {
      // ============= Query construction ============== //
      JpaQueryStream<Person> stream =
          JpaQueryProvider.select(em, Person.class)
              .where()
              .equal(Person::getPersonalNo, Person::getAddrress)
              .and()
              .like(Person::getLastName, GeneralInfo::getFullName, Person::getEscape)
              .and()
              .startsWith(Person::getLastName, "lname");
      String sql = stream.sql();
      System.out.println(sql);
      Person person = stream.firstOrDefault(new Person());
      // =============================================//
      System.out.println();
      System.out.println("-------Entity----");
      System.out.println(person);
    } finally {
      em.close();
    }
  }
Пример #2
0
  @Test
  @RunOrder(10)
  public void queryMaxTest() {

    EntityManager em = emf.createEntityManager();
    try {
      // ============= Query construction ============== //
      JpaQueryStream<Long> stream =
          JpaQueryProvider.select(em, Person.class)
              .where()
              .equal(Person::getPersonalNo, Person::getAddrress)
              .and()
              .like(Person::getLastName, "lname%", 'e')
              .and()
              .startsWith(Person::getLastName, "lname")
              .max(Person::getPersonId);
      String sql = stream.sql();
      System.out.println(sql);
      Long max = stream.firstOrDefault(Long.valueOf(0));
      // =============================================//
      System.out.println();
      System.out.println("-------Entity----");
      System.out.println(max);
    } finally {
      em.close();
    }
  }
Пример #3
0
  @Test
  @RunOrder(5)
  public void deleteByEntityTest() {

    getForUpfate();
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    try {
      transaction.begin();
      // ============= Query construction ============== //
      JpaQueryStream<Person> stream =
          JpaQueryProvider.delete(em, Person.class)
              .where()
              .equal(Person::getPersonalNo, PERSONAL_NO2)
              .and()
              .like(Person::getLastName, "lname%")
              .and()
              .startsWith(Person::getFirstName, "fname");
      int rows = stream.execute();
      // =============================================//
      transaction.commit();
      System.out.println();
      System.out.println("-------Entity----");
      System.out.println();
      System.out.format("deleted %s rows\n", rows);
      Assert.assertEquals("No expected row number was updated", rows, 1);
    } catch (Throwable ex) {
      rollback(transaction);
    } finally {
      em.close();
    }
  }
Пример #4
0
  private void getForUpfate() {

    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    try {
      JpaQueryStream<Person> stream =
          JpaQueryProvider.select(em, Person.class)
              .where()
              .equal(Person::getPersonalNo, PERSONAL_NO2)
              .and()
              .like(Person::getLastName, "lname%")
              .and()
              .startsWith(Person::getFirstName, "fname");
      Person person = stream.getFirst();
      transaction.begin();
      if (person == null) {
        Person newPerson = QueryTest.initPerson();
        em.persist(newPerson);
        person = newPerson;
      }
      transaction.commit();
    } catch (Throwable ex) {
      rollback(transaction);
    } finally {
      em.close();
    }
  }
Пример #5
0
  @Test
  @RunOrder(2.31)
  public void toListBySelectStringTest() {

    EntityManager em = emf.createEntityManager();
    try {
      Date date = getDateValue();
      // ============= Query construction ============== //
      JpaQueryStream<Object[]> stream =
          JpaQueryProvider.select(em, Person.class)
              .where()
              .equal(Person::getPersonalNo, PERSONAL_NO1)
              .and()
              .like(Person::getLastName, "lname%")
              .and()
              .brackets(
                  s ->
                      s.startsWith(Person::getFirstName, "fname")
                          .or()
                          .ge(Person::getBirthDate, date))
              .and()
              .in(Person::getPersonId, Arrays.asList(IDENTIFIERS))
              .select("select c.personalNo, c.firstName, c.lastName");
      System.out.println(stream.sql());
      List<Object[]> persons = stream.toList();
      // =============================================//
      System.out.println();
      System.out.println("-------Entity----");
      System.out.println();
      persons.forEach(c -> System.out.println(Arrays.toString(c)));
    } finally {
      em.close();
    }
  }
Пример #6
0
  @Test
  @RunOrder(1)
  public void supplierEntityTest() {

    EntityManager em = emf.createEntityManager();
    try {
      System.out.println();
      System.out.println("==========Entity============");
      JpaQueryStream<Person> stream = createQueryStream(em);
      System.out.println("===========JPA-QL==========");
      System.out.println();
      System.out.println(stream.sql());
      System.out.println("===========Entity==========");
      System.out.println();
    } finally {
      em.close();
    }
  }
Пример #7
0
  @Test
  @RunOrder(2.36)
  public void toListBySelectConsumerTypedTest() {

    EntityManager em = emf.createEntityManager();
    try {
      Date date = getDateValue();
      // ============= Query construction ============== //
      JpaQueryStream<PersonWrapper> stream =
          JpaQueryProvider.select(em, Person.class)
              .where()
              .equal(Person::getPersonalNo, PERSONAL_NO1)
              .and()
              .like(Person::getLastName, "lname%")
              .and()
              .brackets(
                  s ->
                      s.startsWith(Person::getFirstName, "fname")
                          .or()
                          .ge(Person::getBirthDate, date))
              .and()
              .in(Person::getPersonId, Arrays.asList(IDENTIFIERS))
              .selectType(
                  PersonWrapper.class,
                  s ->
                      s.column(Person::getPersonalNo)
                          .column(Person::getLastName)
                          .column(Person::getFirstName));
      System.out.println(stream.sql());
      List<PersonWrapper> persons = stream.toList();
      // =============================================//
      System.out.println();
      System.out.println("-------Entity----");
      System.out.println();
      persons.forEach(System.out::println);
    } finally {
      em.close();
    }
  }
Пример #8
0
  @Test
  @RunOrder(2.32)
  public void toListBySelectWrapperTest() {

    EntityManager em = emf.createEntityManager();
    try {
      Date date = getDateValue();
      // ============= Query construction ============== //
      JpaQueryStream<PersonWrapper> stream =
          JpaQueryProvider.select(em, Person.class)
              .where()
              .equal(Person::getPersonalNo, PERSONAL_NO1)
              .and()
              .like(Person::getLastName, "lname%")
              .and()
              .brackets(
                  s ->
                      s.startsWith(Person::getFirstName, "fname")
                          .or()
                          .ge(Person::getBirthDate, date))
              .and()
              .in(Person::getPersonId, Arrays.asList(IDENTIFIERS))
              .select(
                  "select new org.lightmare.criteria.entities.PersonWrapper(c.personalNo, c.lastName, c.firstName)",
                  PersonWrapper.class);
      System.out.println(stream.sql());
      List<PersonWrapper> persons = stream.toList();
      // =============================================//
      System.out.println();
      System.out.println("-------Entity----");
      System.out.println();
      persons.forEach(System.out::println);
    } catch (Throwable ex) {
      ex.printStackTrace();
    } finally {
      em.close();
    }
  }
Пример #9
0
  @Test
  @RunOrder(7)
  public void selfQueryTest() {

    EntityManager em = emf.createEntityManager();
    try {
      // ============= Query construction ============== //
      JpaQueryStream<Person> stream =
          JpaQueryProvider.select(em, Person.class)
              .where()
              .equal(Person::getPersonalNo, Person::getAddrress)
              .equal(Person::getFullName, "fullName")
              .like(Person::getFirstName, Person::getFullName)
              .and()
              .startsWith(Person::getLastName, "lname");
      // =============================================//
      System.out.println();
      System.out.println("-------Entity----");
      System.out.println();
      System.out.println(stream.sql());
    } finally {
      em.close();
    }
  }