java - JPA 2.0, Write query with multiple conditionals -
i have entity fields
@entity public myentity { @id @generatedvalue(strategy = generationtype.identity) public long id; public uuid uuid; public string value; ..... }
now create query returns 1 if there @ least x entities in database same uuid, or returns 2 if there @ least y entities same uuid , have same value. im not sure if described well. if in java collection following code:
int b = 0; string y = ..; uuid x = ...; list<myentity> list = getallentitieswithsameuuid(x) if (list.sze() == 50) return 1 (myentity e : list) { if (e.value.equalsignorecase(y)) { b++; if (b == 100) { return 2; } } }
in case have process tons of data, move processing database server , in application retrieve integer result of query.
can use jpa's query, criteriaquery, (something else) achieve this? i'm using jpa 2.0, , hibernate 4.
i use 2 queries , combine both in java:
string value = ..; uuid uuid = ...; number uuidcount = entitymanager.createquery( "select count(e) myentity e e.uuid = :uuid").setparameter( "uuid", uuid).getsingleresult(); if (uuidcount.longvalue() == 50) { return 1; } number uuidandvaluecount = entitymanager.createquery( "select count(e) myentity e e.uuid = :uuid , e.value = :value").setparameter( "uuid", uuid).setparameter("value", value).getsingleresult(); if (uuidandvaluecount.longvalue() == 100) { return 2; }
i think performance difference of 1 catch all query compared 2 separate queries minimal. depending on execution plan may happen solution faster, need fire second query only, if first fails.
Comments
Post a Comment