users@glassfish.java.net

Re: Toplink's em.getReference - terrible :/

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Sat, 23 Jun 2007 03:33:49 +0200

2007/6/23, glassfish_at_javadesktop.org <glassfish_at_javadesktop.org>:
> Well, I won't comment on how horrible the whole statement is, because I'm confident that it's an example to prove a point rather an idiom you're practicing in code.
> Everyone knows for large queries like that you should be selecting as much as you
> can in a single network hit through joins rather than making a gazillion
> network hits. Common sense.

THE statement was only to show the problem, it is the real statement we do use.
You must be really out of any reality to say what you said. The
em.getReference SHOULD NOT produce any query, as it is only to provide
some kind of >stub< (for example for use in query.setParameter(xxx,
em.getReference(SomeClass.class, someID);

Many times, it is necessary to provide only a REFERENCE to an object,
which is what em.getReference should provide, without actually
fetching anything from database.

> For example:
> [code]
> Query q = em.createQuery("SELECT l FROM Loan l");
> for (Loan l : q.getResultList()) {
> ....
> }
> [/code]
> will run in a heartbeat compared to what you posted.

NO, because I am REALLY not interested in fetching 70 thousand loans in a query.

> If the object by the primary key is within either the level 1 or level 2 cache, find will NOT hit the DB.

Consider this: I am not really interested in fetching given loan at all...

> Anyway, bottom line, the EM is doing everything properly. It's a feature, not a bug.

As long as it is really trying to fetch all the data for loans I am
not really interested in, I am not happy. With plain SQL I can get
LoanID using only one single "long" and that is really enough to
provide a foreign key to any other statement. Current em.getReference
is invoking 4 or 5 huge queries into database, only to give me the ID
which is the only thing I need.

>I consider the example you posted bad form for any data set of reasonable size.

As I said, you have probably never had to write any complex
application with JPA QL. Anything other than just a simple tutorial
will show you the need for getting a _reference_ to entities.