JPA Query Problem

Hi. I've got a problem with a JPQL query.

SELECT o FROM Charter AS o WHERE 1=1

Siple, but it doesn't work. Instead of returning all the rows one time, it returns all the rows N times, where N is the number of rows in the table.

WHY? Do I need to check my annotations in the Entity class?

Quite new to JPA.

Thank you,

Corrado

[369 byte] By [Konrad84a] at [2007-11-27 5:36:55]
# 1
Dont use WHERE Clausethe JPA Query should be SELECT o FROM Charter o Thanks
amarpolimeraa at 2007-7-12 15:08:27 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Hi, thanks.Of course without the where clause, the query works correctly, but i would like to know why, with the where clause that should change nothing in term of results, bring to that strange behaviour. I think it's a bug of toplink implmentation.
Konrad84a at 2007-7-12 15:08:27 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
yes, why cant you raise a ticket with toplink community?
amarpolimeraa at 2007-7-12 15:08:27 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
I wanted to ask someone before, maybe I was missing something basilar.. but now i'm definetely going to post a bug-report. Thank you
Konrad84a at 2007-7-12 15:08:27 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
Do you have some sort of intersection table in your database? Maybe if you posted an overview of your database schema it would help.
orbacha at 2007-7-12 15:08:27 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

Yes. In fact, the problem I've posted is a generalization of a more strange behaviur. The (cutted-off) schema is

Table Charter

idintegerprimary key

company integerforeign key (on 'id' of table 'company')

pricedecimal

Table Company

idintegerprimary key

nametext

Now, assume I have 10 records in table Charter referencing 10 records in table Company (each row in Charter reference a distinct row in Company).

This query returns me THE CORRECT RESULT (that is, each matching row is present in the result set only once):

SELECT o FROM Charter AS o WHERE o.company.id = :company AND 1=1

Instead, this query returns me each matching row, BUT REPEATED N TIMES, where N is the number of rows in Charter (10):

SELECT o FROM Charter AS o WHERE 1=1 AND o.company.id = :company

(Notice that the only difference is the order of the where clause)

(Notice also that I get the same behaviour even if I do a query on the 'price' field).

Also, this query that should return all the rows in the table (one time each), returns them N times:

SELECT o FROM Charter AS o WHERE 1=1

Any idea?

Really thank you

ps I forgot to say that, after translating those JPQL query in standard SQL and executing them in PostgreSQL console, they are, obiouvsly, equivalent and work correctly.

Message was edited by:

Konrad84

Konrad84a at 2007-7-12 15:08:27 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

I cannot give you a sure answer but I think this behavior has to do with the way Persistence Providers issue SQL statements to gather related records. Although your JPQL statement seems to reference a single table, internally toplink will issue some sort of join to gather related records. I am just guessing here and a TopLink forum may be better for this sort of question.

You can configure TopLink logging to output more information, hopefully it will output the actual SQL statement it issues. See:

http://www.oracle.com/technology/products/ias/toplink/jpa/howto/configure-logging.html

to learn how to configure TopLink logging.

orbacha at 2007-7-12 15:08:27 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...