Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findRowCount doesn't work when bean has property with @Formula annotation

I have following class:

@Entity
@Table(name = "clients")
public class Client extends Model {
    @Id
    public int id;

    @Formula(select = "inv.some_data", 
            join = "left join (select 1 as some_data) as inv")
    public int someData;

    public static Finder<String, Client> find = 
        new Finder<String, Client>(String.class, Client.class);

    public static int countClientsWithData() {
        return Client.find.where().gt("someData", 0).findRowCount();
    }
}

It has someData field (play framework will generate getters and setters automatically). And also countClientsWithData uses this field in where clause. Now if I do

int count = Client.countClientsWithData();

It will throw NullPointerException while trying to execute query

select count(*) from clients t0 where inv.some_data > ?

Looks like findRowCount doesn't recognize join in @Formula annotation. Any thoughts on how to work around this problem?

Updated question: narrowed down problem to findRowCount call.

like image 779
Poma Avatar asked Sep 14 '12 09:09

Poma


1 Answers

So you want the count without using findRowCount() method and without fetching all the data ..

Solution: Copy the same query, and make it on the form select count(*) from .. and use it to find the count

Example:

If your query is on the form ..

Query: SELECT * FROM clients

Then this line of code Client.find.where().gt("some_data", 0).findRowCount(); will be equivlant to ..

Count Query: SELECT COUNT(*) FROM clients WHERE some_data > 0

like image 169
Khaled.K Avatar answered Oct 11 '22 07:10

Khaled.K