示例#1
0
  @Test
  public void querydsl() {
    JPAQuery<Person> q = new JPAQuery<Person>(em);

    QPerson p = QPerson.person;

    List<Person> people = q.select(p).from(p).where(p.name.toLowerCase().like("p%")).fetch();

    assertThat(people, hasSize(10));
  }
示例#2
0
  @Override
  public boolean isValid(LocationModel value, ConstraintValidatorContext context) {
    Long id = value.getId();
    QLocation location = QLocation.location;
    JPAQuery qquery = new JPAQuery(entityManager);
    qquery.from(location);
    if (id != null) {
      qquery.where((location.id.ne(value.getId())));
    }
    String param = null;
    BooleanExpression exp = null;
    if (value.getIata() != null) {
      exp = location.iata.eq(value.getIata());
      param = "iata";
    }
    if (value.getIcao() != null) {
      if (exp == null) exp = location.icao.eq(value.getIcao());
      else exp = exp.or(location.icao.eq(value.getIcao()));
      param = "icao";
    }
    if (value.getIso() != null) {
      if (exp == null) exp = location.iso.eq(value.getIso());
      else exp = exp.or(location.iso.eq(value.getIso()));
      param = "iso";
    }
    if (value.getITransfer() != null) {
      if (exp == null) exp = location.iTransfer.eq(value.getITransfer());
      else exp = exp.or(location.iTransfer.eq(value.getITransfer()));
      param = "itransfer";
    }
    if (value.getUfs() != null) {
      if (exp == null) exp = location.ufs.eq(value.getUfs());
      else exp = exp.or(location.ufs.eq(value.getUfs()));
      param = "ufs";
    }
    qquery.where(exp);
    List results = qquery.createQuery().getResultList();

    if (results.size() > 0) {
      context
          .buildConstraintViolationWithTemplate("Unique constraint violation")
          .addPropertyNode(param)
          .addConstraintViolation();
      context.disableDefaultConstraintViolation();

      return false;
    }
    return true;
  }