Playframework - querying and displaying results fr

2019-07-11 04:56发布

I'm a newcomer to java and the play framework (I'm using play 1.2.2 with a local MySQL5 database). I'm trying to query a couple of tables in a database and display the table join results on a web page.

This is what I have in the various bits:

Controller :-

public static void index() {
        List<Mutation> mutation_list= Mutation.getDisorderGene();
        render(mutation_list);       
    }

Model :-

public class Mutation extends Model {

  public static List<Mutation> getDisorderGene() {
    EntityManager entityManager = play.db.jpa.JPA.em();
    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;
  }

View :-

#{list items:mutation_list, as:'mutation'}
                    <tr>
                        <td>${mutation.disorder_name}</td>
                        <td>${mutation.gene_name}</td>
                    </tr>
 #{/list}            

And this is the error message I get!

Template execution error

Execution error occured in template /app/views/Stu/index.html. Exception raised was MissingPropertyException : Exception evaluating property 'disorder_name' for java.util.Arrays$ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: disorder_name for class: java.lang.String.

I'm unsure where the problem lies. Is it with the JPA table join query or is something wrong in the view.

Are there any changes in the view I can make to display the columns?

Many thanks.

2条回答
太酷不给撩
2楼-- · 2019-07-11 05:08

What if you put the as clausule...

select disorder_name as NameMyPropertyModelClass, gene_name as NameMyPropertyModelClass 
from  Disorder,Mutation 
where Disorder.id = Mutation.disorder_id order by disorder_name, gene_name
查看更多
Ridiculous、
3楼-- · 2019-07-11 05:12

A native query that selects multiple columns by default returns them as Object[], thus your getDisorderGene() should return a List<Object[]>, and your template should look as follows:

#{list items:mutation_list, as:'mutation'}
                    <tr>
                        <td>${mutation[0]}</td>
                        <td>${mutation[1]}</td>
                    </tr>
 #{/list}  
查看更多
登录 后发表回答