[play-framework] [1.2.5] JSON response different when querying using JPA SQL Versus using finder methods.

103 views
Skip to first unread message

Gokul Pillai

unread,
Sep 24, 2012, 3:01:41 AM9/24/12
to play-fr...@googlegroups.com
I find that given a Model "Order" with fields in the database table orderNo, amount, date, there is a different JSON being output when query using
1. Order.find("byDate",mydate)
This gives the response:
[
{
orderNo: 123456
amount: 34.56
date: 09-08-2012
}
]

Whereas if I used this construct:
2. Order.find("select orderNo, amount, date from Order where date =?",mydate)
In this case I get the JSON without the key-names which looks like this:
[
{
123456
34.56
09-08-2012
}
]

How can I make sure the JSON is rendered with the "key-names" as well since this is important to the client.

Thanks
Gokul

Tom Carchrae

unread,
Sep 24, 2012, 1:04:25 PM9/24/12
to play-fr...@googlegroups.com
neither example is valid json. post the rest of the controller code.
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.

Gokul Pillai

unread,
Sep 24, 2012, 1:46:55 PM9/24/12
to play-fr...@googlegroups.com
Hi Tom
Here is the controller code in either case with the response received:

public static void ordersByDate(String mydate) {
        List<Order> orders = Order.find("SELECT orderNo,amount" +
                " from Orders where date = ?", mydate).from(0)
                .fetch(50);
        renderJSON(orders);
    }

Response is :
    [
        [
            174434,
            252
        ],
        [
            174479,
            124
        ]
    ]

    public static void ordersByDateJpa(String mydate) {
            List<Order> orders = Order.find("byDate", mydate).from(0)
                    .fetch(50);
            renderJSON(orders);
        }

Response in this case:
[

    {
        "orderNo": 174434,
        "amount": 252
    },
    {
        "orderNo": 174479,
        "amount": 124
    }
]

In the first case, I believe List<Object> is being returned by the "find" method and in the second case, List<Order>.
So how do I make sure even for the first case, List<Order> gets returned other than looping through the List<Object> and casting it to "Order" type explicitly.

-Gokul

Tom Carchrae

unread,
Sep 24, 2012, 1:49:51 PM9/24/12
to play-fr...@googlegroups.com
I would guess this is not actually a List<Order> being returned. If
you debug, you'll probably find it is a List<List<Object>>. Does it
make sense that you can return part of the Order bean? Not with JPA
afaik.

Remove the text "SELECT orderNo,amount" from your query and try again.
It should work as you want then.

Gokul Pillai

unread,
Sep 24, 2012, 2:12:57 PM9/24/12
to play-fr...@googlegroups.com
Got it. That was the issue. I removed the SELECT orderNo,amount and it works the same way now.
Thank you Tom ! I will read up more on how JPA works.
Reply all
Reply to author
Forward
0 new messages