jeudi 13 août 2015

Raw mysql queries with all the clause without writing every attribute in play framework (java)

So In Java (play framework) I want to write a raw MySQL query to get all attributes from myTable with a WHERE clause (and possibly other clauses)

I am using RawSqlBuilder. If I use unparsed, then I Cannot have any conditional clause (such as where). See example below

String sql = "SELECT * FROM myTable"; // here i CANNOT add a WHERE atOne = 5 since I am using unparsed. It doesn't work

RawSql rawSql =   
                 RawSqlBuilder  
                    .unparsed(sql)
                    .columnMapping("attOne", "attOne")
                    .columnMapping("attTwo", "attTwo")
                    .create();  

Query<myTableModel> query = Ebean.find(myTableModel.class);
query.setRawSql(rawSql);

List<myTableModel> list = query.findList();

However, if I use parsed, then I CAN use my WHERE clause but I Cannot use * after my Select. Instead of * I must write ALL the attributes names that I want to get. But this is VERY inconvenient. What if I have 25 attributes? example below

String sql = "SELECT attOne, attTwo FROM myTable WHERE attOne = 5"; // as you can see here, I MUST put attOne, attTwo else it won't work

RawSql rawSql =   
                 RawSqlBuilder  
                    .parsed(sql)
                    .columnMapping("attOne", "attOne")
                    .columnMapping("attTwo", "attTwo")
                    .create();  

Query<myTableModel> query = Ebean.find(myTableModel.class);
query.setRawSql(rawSql);

List<myTableModel> list = query.findList();

My question is, how can I use * along with WHERE clause and map it to my myTableModel

If I can't, is there a way to access sql db with raw queries in java?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire