You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Mauro Flores <ma...@procergs.rs.gov.br> on 2009/12/16 18:31:33 UTC

COUNT(t) to COUNT(*)

The  jpql:  "SELECT count(o) FROM Object o" (A)

is usually translated to this sql: "SELECT count(t.primarykey) FROM Table t"
(B)

I'm working with a legacy database witch jdbc driver doesn't accept this
format of sql. 
It works with: "SELECT count(*) FROM Tabelat" (C) 

Is there a way to make the query C (with count(*)) be generated instead of
query B? 

I'm using the default jpa dictionary.  I know that I can extend DBDictionary
in order to customize this. 
I had some difficulty trying to do this. 

Somebody would give some hint? 

Thanks. Mauro. 

-- 
View this message in context: http://n2.nabble.com/COUNT-t-to-COUNT-tp4176827p4176827.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: COUNT(t) to COUNT(*)

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  Added to support COUNT(*) for simple queries with a database dictionary
option.
SVN revision 892947 on trunk. Also see OPENJPA-1440.

  Use as follows:
 <property name="openjpa.jdbc.DBDictionary"
value="useWildCardForCount=true"/>



Mauro Flores wrote:
> 
> The  jpql:  "SELECT count(o) FROM Object o" (A)
> 
> is usually translated to this sql: "SELECT count(t.primarykey) FROM Table
> t" (B)
> 
> I'm working with a legacy database witch jdbc driver doesn't accept this
> format of sql. 
> It works with: "SELECT count(*) FROM Tabelat" (C) 
> 
> Is there a way to make the query C (with count(*)) be generated instead of
> query B? 
> 
> I'm using the default jpa dictionary.  I know that I can extend
> DBDictionary in order to customize this. 
> I had some difficulty trying to do this. 
> 
> Somebody would give some hint? 
> 
> Thanks. Mauro. 
> 
> 


-----
Pinaki 
-- 
View this message in context: http://n2.nabble.com/COUNT-t-to-COUNT-tp4176827p4208818.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: COUNT(t) to COUNT(*)

Posted by Judes Tumuhairwe <ju...@gmail.com>.
Mauro,
If it's just a count, another option would be to just use a
@NamedNativeQuery(name="SELECT count(*) FROM Tabelat") instead of using
JPQL. If this is an option, this might be a lot simpler/easier than
extending a class.

regards,
Judes Tumuhairwe



On Thu, Dec 17, 2009 at 2:48 PM, Miłosz Tylenda <mt...@o2.pl> wrote:

> Hi Mario,
>
> You are right. The processing of your JPQL query does not go through the
> method I suggested. Instead, it is handled in classes from the
> org.apache.openjpa.jdbc.kernel.exps package - mainly Count class and
> UnaryOp.appendTo method. You could probably modify the appendTo method to
> suit your need. However, I guess you don't want to modify the OpenJPA source
> code but provide some extension only. This might be impossible for these
> classes, at least I don't know the way to do it.
>
> My other idea then is to look at getSelects and toSelect methods in
> DBDictionary.
>
> Hope this helps,
> Milosz
>
>
> >
> >
> > I  had tried it already, Milosz.
> > But this overwritten method doesn't seem to be running. I debugged it,
> > others overwritten methods runs.
> >
> >
> >
> > Miłosz Tylenda wrote:
> > >
> > > Mauro,
> > >
> > > I would start with looking at DBDictionary.toSelectCount method.
> > >
> > > Regards,
> > > Milosz
> > >
> > >
> > >> The  jpql:  "SELECT count(o) FROM Object o" (A)
> > >>
> > >> is usually translated to this sql: "SELECT count(t.primarykey) FROM
> Table
> > >> t"
> > >> (B)
> > >>
> > >> I'm working with a legacy database witch jdbc driver doesn't accept
> this
> > >> format of sql.
> > >> It works with: "SELECT count(*) FROM Tabelat" (C)
> > >>
> > >> Is there a way to make the query C (with count(*)) be generated
> instead
> > >> of
> > >> query B?
> > >>
> > >> I'm using the default jpa dictionary.  I know that I can extend
> > >> DBDictionary
> > >> in order to customize this.
> > >> I had some difficulty trying to do this.
> > >>
> > >> Somebody would give some hint?
> > >>
> > >> Thanks. Mauro.
> > >>
> > >> --
> > >> View this message in context:
> > >> http://n2.nabble.com/COUNT-t-to-COUNT-tp4176827p4176827.html
> > >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> > >
> > >
> > >
> >
> > --
> > View this message in context:
> http://n2.nabble.com/COUNT-t-to-COUNT-tp4176827p4182603.html
> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >
>

Re: COUNT(t) to COUNT(*)

Posted by Miłosz Tylenda <mt...@o2.pl>.
Hi Mario,

You are right. The processing of your JPQL query does not go through the method I suggested. Instead, it is handled in classes from the org.apache.openjpa.jdbc.kernel.exps package - mainly Count class and UnaryOp.appendTo method. You could probably modify the appendTo method to suit your need. However, I guess you don't want to modify the OpenJPA source code but provide some extension only. This might be impossible for these classes, at least I don't know the way to do it.

My other idea then is to look at getSelects and toSelect methods in DBDictionary.

Hope this helps,
Milosz


> 
> 
> I  had tried it already, Milosz. 
> But this overwritten method doesn't seem to be running. I debugged it,
> others overwritten methods runs. 
> 
> 
> 
> Miłosz Tylenda wrote:
> > 
> > Mauro,
> > 
> > I would start with looking at DBDictionary.toSelectCount method.
> > 
> > Regards,
> > Milosz
> > 
> > 
> >> The  jpql:  "SELECT count(o) FROM Object o" (A)
> >> 
> >> is usually translated to this sql: "SELECT count(t.primarykey) FROM Table
> >> t"
> >> (B)
> >> 
> >> I'm working with a legacy database witch jdbc driver doesn't accept this
> >> format of sql. 
> >> It works with: "SELECT count(*) FROM Tabelat" (C) 
> >> 
> >> Is there a way to make the query C (with count(*)) be generated instead
> >> of
> >> query B? 
> >> 
> >> I'm using the default jpa dictionary.  I know that I can extend
> >> DBDictionary
> >> in order to customize this. 
> >> I had some difficulty trying to do this. 
> >> 
> >> Somebody would give some hint? 
> >> 
> >> Thanks. Mauro. 
> >> 
> >> -- 
> >> View this message in context:
> >> http://n2.nabble.com/COUNT-t-to-COUNT-tp4176827p4176827.html
> >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> > 
> > 
> > 
> 
> -- 
> View this message in context: http://n2.nabble.com/COUNT-t-to-COUNT-tp4176827p4182603.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> 

Re: COUNT(t) to COUNT(*)

Posted by Mauro Flores <ma...@procergs.rs.gov.br>.

I  had tried it already, Milosz. 
But this overwritten method doesn't seem to be running. I debugged it,
others overwritten methods runs. 



Miłosz Tylenda wrote:
> 
> Mauro,
> 
> I would start with looking at DBDictionary.toSelectCount method.
> 
> Regards,
> Milosz
> 
> 
>> The  jpql:  "SELECT count(o) FROM Object o" (A)
>> 
>> is usually translated to this sql: "SELECT count(t.primarykey) FROM Table
>> t"
>> (B)
>> 
>> I'm working with a legacy database witch jdbc driver doesn't accept this
>> format of sql. 
>> It works with: "SELECT count(*) FROM Tabelat" (C) 
>> 
>> Is there a way to make the query C (with count(*)) be generated instead
>> of
>> query B? 
>> 
>> I'm using the default jpa dictionary.  I know that I can extend
>> DBDictionary
>> in order to customize this. 
>> I had some difficulty trying to do this. 
>> 
>> Somebody would give some hint? 
>> 
>> Thanks. Mauro. 
>> 
>> -- 
>> View this message in context:
>> http://n2.nabble.com/COUNT-t-to-COUNT-tp4176827p4176827.html
>> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/COUNT-t-to-COUNT-tp4176827p4182603.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: COUNT(t) to COUNT(*)

Posted by Miłosz <mt...@o2.pl>.
Mauro,

I would start with looking at DBDictionary.toSelectCount method.

Regards,
Milosz


> The  jpql:  "SELECT count(o) FROM Object o" (A)
> 
> is usually translated to this sql: "SELECT count(t.primarykey) FROM Table t"
> (B)
> 
> I'm working with a legacy database witch jdbc driver doesn't accept this
> format of sql. 
> It works with: "SELECT count(*) FROM Tabelat" (C) 
> 
> Is there a way to make the query C (with count(*)) be generated instead of
> query B? 
> 
> I'm using the default jpa dictionary.  I know that I can extend DBDictionary
> in order to customize this. 
> I had some difficulty trying to do this. 
> 
> Somebody would give some hint? 
> 
> Thanks. Mauro. 
> 
> -- 
> View this message in context: http://n2.nabble.com/COUNT-t-to-COUNT-tp4176827p4176827.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.