質問

I am working on some inherited code and I am not use to the entity frame work. I'm trying to figure out why a previous programmer coded things the way they did, sometimes mixing and matching different ways of querying data.

Deal d = _em.find(Deal.class, dealid);
List<DealOptions> dos = d.getDealOptions();
for(DealOptions o : dos) {
    if(o.price == "100") {
       //found the 1 item i wanted
    }
}

And then sometimes i see this:

Query q = _em.createQuery("select count(o.id) from DealOptions o where o.price = 100 and o.deal.dealid = :dealid");
//set parameters, get results then check result and do whatver

I understand what both pieces of code do, and I understand that given a large dataset, the second way is more efficient. However, given only a few records, is there any reason not to do a query vs just letting the entity do the join and looping over your recordset?

役に立ちましたか?

解決

Some reasons never to use the first approach regardless of the number of records:

  • It is more verbose

  • The intention is less clear, since there is more clutter

  • The performance is worse, probably starting to degrade with the very first entities

  • The performance of the first approach will degrade much more with each added entity than with the second approach

  • It is unexpected - most experienced developers would not do it - so it needs more cognitive effort for other developers to understand. They would assume you were doing it for a compelling reason and would look for that reason without finding one.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top