private EntityManager entityManager;
@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("select cat\nfrom Cat cat\nwhere cat.name = ?1", query2.toString());
query2.list(cat);
}