@Test
  public void test() throws IOException, ClassNotFoundException {
    // create query
    JPAQuery query = new JPAQuery(entityManager);
    query.from(cat).where(cat.name.eq("Kate")).list(cat);

    // get metadata
    QueryMetadata metadata = query.getMetadata();
    assertFalse(metadata.getJoins().isEmpty());
    assertTrue(metadata.getWhere() != null);
    assertTrue(metadata.getProjection().isEmpty());

    // serialize metadata
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(metadata);
    out.close();

    // deserialize metadata
    ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream in = new ObjectInputStream(bain);
    QueryMetadata metadata2 = (QueryMetadata) in.readObject();
    in.close();

    // validate it
    assertEquals(metadata.getJoins(), metadata2.getJoins());
    assertEquals(metadata.getWhere(), metadata2.getWhere());
    assertEquals(metadata.getProjection(), metadata2.getProjection());

    // create new query
    JPAQuery query2 = new JPAQuery(entityManager, metadata2);
    assertEquals("from Cat cat\nwhere cat.name = :a1", query2.toString());
    query2.list(cat);
  }
Example #2
0
  @Override
  public void serialize(QueryMetadata metadata, boolean forCountRow, SQLSerializer context) {
    if (!forCountRow && metadata.getModifiers().isRestricting() && !metadata.getJoins().isEmpty()) {
      QueryModifiers mod = metadata.getModifiers();
      if (mod.getOffset() == null) {
        // select top ...
        metadata = metadata.clone();
        metadata.addFlag(
            new QueryFlag(
                QueryFlag.Position.AFTER_SELECT,
                Expressions.template(Integer.class, topTemplate, mod.getLimit())));
        context.serializeForQuery(metadata, forCountRow);
      } else {
        throw new IllegalStateException("offset not supported");
      }

    } else {
      context.serializeForQuery(metadata, forCountRow);
    }

    if (!metadata.getFlags().isEmpty()) {
      context.serialize(Position.END, metadata.getFlags());
    }
  }