Printing results from multi-table join using createNativeQuery

363 views
Skip to first unread message

curly

unread,
Oct 5, 2011, 6:44:46 AM10/5/11
to play-framework
Hello,

I'm new to java and play. I would like to print out the results from a
sql createNativeQuery query in java using System.print.

My Controller calls a particular method:
List<Mutation> mutation_list= Mutation.getDisorderGene();

The model has the query:
List<Mutation> muts = entityManager.createNativeQuery("select
disorder_name, gene_name from Disorder,Mutation where Disorder.id =
Mutation.disorder_id order by disorder_name,
gene_name").getResultList();
return muts;

I can view this with groovy:
#{list items:muts, as:'m'}
<tr>
<td>${m[0]}</td>
<td>${m[1]}</td>
</tr>
#{/list}

However, what I'd like to do is print the muts list out with
System.println in the controller.

I've tried:

for(Mutation m: muts) {
System.out.println("OUTPUT= " + m );
}

and:

while (muts.next()) {
System.out.println(muts);
}

But all to no avail! Am I making a basic error?!

Many thanks.

Fehmi Can SAĞLAM

unread,
Oct 5, 2011, 7:51:02 AM10/5/11
to play-fr...@googlegroups.com

10/5/11 1:44 PM, curly yazmış:
> Hello,
Hi,

>
> I'm new to java and play. I would like to print out the results from a
> sql createNativeQuery query in java using System.print.
>
> My Controller calls a particular method:
> List<Mutation> mutation_list= Mutation.getDisorderGene();
>
> The model has the query:
> List<Mutation> muts = entityManager.createNativeQuery("select
> disorder_name, gene_name from Disorder,Mutation where Disorder.id =
> Mutation.disorder_id order by disorder_name,
> gene_name").getResultList();
> return muts;
I think there is a logical mistake here. How can a native query return a
model list? Maybe it is possible though.
I would do like this:
List<Object[]> muts = entityManager.createNativeQuery("....

>
> I can view this with groovy:
> #{list items:muts, as:'m'}
> <tr>
> <td>${m[0]}</td>
> <td>${m[1]}</td>
> </tr>
> #{/list}
>
> However, what I'd like to do is print the muts list out with
> System.println in the controller.
>
> I've tried:
>
> for(Mutation m: muts) {
> System.out.println("OUTPUT= " + m );
> }

Then print like this:
for(Object []m: muts) {
System.out.println("OUTPUT= " + m[0] + " " + m[1] );

curly

unread,
Oct 5, 2011, 8:38:16 AM10/5/11
to play-framework
Hi Fhemi,

Thanks very much - it works!


On Oct 5, 12:51 pm, Fehmi Can SAĞLAM <fehmican.sag...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages