I've created a very simple page which executes your JPQL query and shows the results as JSON, using GSON.

It has no features besides a textarea and the formatted output.

Here it is.


Screenshot from 2013-01-24 02:49:50.png


Before the code, let me warn you that GSON can't deal with circular references on it's own.

You need to cut them at appropriate places, by either using @XmlTransient to prevent that particular member to be serialized,

or by serializing it differently, e.g. using toString(). I think GSON follows most, if not all, JAXB annotations, so that's the way.


First, add GSON to classpath.


<!-- GSON - for JpaQueryPage -->


And here is the Wicket page - HTML and Java.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
        <title>JPA Query Page</title>
        <form wicket:id="form">
            <textarea wicket:id="query" style="min-width: 60em;" rows="5">SELECT p FROM Product p</textarea>
            <br/> Example: <code>SELECT p FROM Product p LEFT JOIN FETCH p.customFields</code>
            <input type="submit" value="Perform"/>

            <table style="font-family: monospace; font-size: 8pt;">
                <tr wicket:id="result">
                    <td wicket:id="item" style="white-space: pre">Some query result...</td>



package org.jboss.essc.web.pages.test;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.jboss.essc.web.model.ProductCustomField;
import org.jboss.essc.web.model.ReleaseCustomField;

 *  @author Ondrej Zizka
public class JpaQueryPage extends WebPage {

    @PersistenceContext EntityManager em;

    private String query = "";

    private List<Object> result = Collections.EMPTY_LIST;

    public JpaQueryPage() {

        Form form = new Form("form", new CompoundPropertyModel(this) ){
            @Override protected void onSubmit() {
                try {
                    result = em.createQuery( query ).getResultList();
                catch (Exception ex){
                    result = new ArrayList(2);
                    result.add( ex );
                    result.add( query );
        add( form );

        form.add( new TextArea("query") );
        this.query = 
                "SELECT rel, rel.product FROM Release rel\n"
                + " LEFT JOIN FETCH rel.product.customFields\n"
                + " LEFT OUTER JOIN FETCH rel.customFields\n"
                + " WHERE rel.product.name = 'EAP' AND rel.version = '6.0.1.GA'";

        form.add( new ListView("result") {
            protected void populateItem( ListItem item ) {
                String content;
                try {
                    content = toJSON( item.getModelObject() );
                } catch (Throwable ex){
                    try {
                        content = item.getModelObject().toString();
                    } catch (Exception ex2) {
                        content = item.getModelObject().getClass() + " but toString() threw: " + ex2.toString();
                item.add( new Label("item", content) );

        } );

    }// const

     *  Convert any object to JSON.
    private static String toJSON( Object object ) {
        //Gson gson = new Gson();
        Gson gson = new GsonBuilder()
                .addDeserializationExclusionStrategy( new ExclusionStrategy() {
                    @Override public boolean shouldSkipField( FieldAttributes f ) {
                        return false;
                    @Override public boolean shouldSkipClass( Class<?> clazz ) {
                        if( ReleaseCustomField.class.equals( clazz ) )
                                return true;
                        if( ProductCustomField.class.equals( clazz ) )
                                return true;
                        return false;
                } )
        return gson.toJson(object);

}// class