FAQ
Hi all.

I have a utility method that selects a row count given a DataObject class and an Expression, as seen here:

https://gist.github.com/hugith/d2cdaf60a6ee12aa0d17 <https://gist.github.com/hugith/d2cdaf60a6ee12aa0d17>

I was using this today when I suddenly got the error message "the scalar type 'Date' is not supported as a scalar type in EJBQL”. Turns out that if I use a date in an Expression, it can’t be converted to EJBQL.

Can anyone suggest a workaround? Also, should I be creating utility methods like this in a different way than by using EJBQL? I have quite a few utility methods that all construct queries using EJBQL, but they become rather useless if I can’t use dates in them.

Cheers and thanks,
- hugi

Search Discussions

  • Andrew Lindesay at Aug 12, 2015 at 5:37 am
    Hi Hugi;

    I think this was me; see CAY-1932 for some details on what I did there.

    cheers.
    On 12/08/15 10:09, Hugi Thordarson wrote:
    Hi all.

    I have a utility method that selects a row count given a DataObject class and an Expression, as seen here:

    https://gist.github.com/hugith/d2cdaf60a6ee12aa0d17 <https://gist.github.com/hugith/d2cdaf60a6ee12aa0d17>

    I was using this today when I suddenly got the error message "the scalar type 'Date' is not supported as a scalar type in EJBQL”. Turns out that if I use a date in an Expression, it can’t be converted to EJBQL.

    Can anyone suggest a workaround? Also, should I be creating utility methods like this in a different way than by using EJBQL? I have quite a few utility methods that all construct queries using EJBQL, but they become rather useless if I can’t use dates in them.

    Cheers and thanks,
    - hugi

    --
    Andrew Lindesay
  • Hugi Thordarson at Aug 12, 2015 at 8:35 am
    Hi Andrew,
    thanks for that. I see what the problem is.

    But if I can’t use EJBQL when generating queries that use dates, can I go down any different paths or do I have to revert to writing plain old SQL?

    Cheers,
    - hugi


    On 12. ágú. 2015, at 05:36, Andrew Lindesay wrote:

    Hi Hugi;

    I think this was me; see CAY-1932 for some details on what I did there.

    cheers.
    On 12/08/15 10:09, Hugi Thordarson wrote:
    Hi all.

    I have a utility method that selects a row count given a DataObject class and an Expression, as seen here:

    https://gist.github.com/hugith/d2cdaf60a6ee12aa0d17 <https://gist.github.com/hugith/d2cdaf60a6ee12aa0d17>

    I was using this today when I suddenly got the error message "the scalar type 'Date' is not supported as a scalar type in EJBQL”. Turns out that if I use a date in an Expression, it can’t be converted to EJBQL.

    Can anyone suggest a workaround? Also, should I be creating utility methods like this in a different way than by using EJBQL? I have quite a few utility methods that all construct queries using EJBQL, but they become rather useless if I can’t use dates in them.

    Cheers and thanks,
    - hugi

    --
    Andrew Lindesay
  • Andrew Lindesay at Aug 12, 2015 at 9:21 am
    Hi Hugi;

    You can use EJBQL queries with timestamps, but there is presently no
    supported _literal_ representation for a timestamp inside the query
    string so you have to use parameters instead. Here is an example;

        EJBQLQuery q = new EJBQLQuery(String.format(
          "DELETE FROM %s r WHERE r.createTimestamp < :expiryTimestamp",
          Response.class.getSimpleName()));

        q.setParameter("expiryTimestamp",
          new Timestamp(System.currentTimeMillis() -
          TimeUnit.SECONDS.toMillis(expirySeconds)));

        getServerRuntime().getContext().performQuery(q);

    To help with getting Expression objects to over to EJBQL with timestamp
    literals;

        Expression#toEJBQL(List<Object> parameterAccumulator, String rootId)

    So you call that and as it creates the EJBQL fragment, it will insert
    any necessary parameters into the parameterAccumulator for you all set
    to go into the EJBQLQuery.

    Hopefully that helps?

    cheers.

    [1]
    https://github.com/aplgithub/haikudepotserver/blob/master/haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/captcha/DatabaseCaptchaRepository.java


        For example;

        SELECT f FROM Foo f WHERE f.createTimestamp > ?

    Then you just need to use the

    On 12/08/15 20:35, Hugi Thordarson wrote:
    Hi Andrew,
    thanks for that. I see what the problem is.
    But if I can’t use EJBQL when generating queries that use dates, can I go down any different paths or do I have to revert to writing plain old SQL?
    --
    Andrew Lindesay
  • Hugi Thordarson at Aug 12, 2015 at 10:29 am
    That’s an awesome solution you created there Andrew, works like a charm. Thanks yet again!

    Happy little counting method -> https://gist.github.com/hugith/8a21aa4247385732a4ce

    Cheers,
    - hugi



    On 12. ágú. 2015, at 09:20, Andrew Lindesay wrote:

    Hi Hugi;

    You can use EJBQL queries with timestamps, but there is presently no supported _literal_ representation for a timestamp inside the query string so you have to use parameters instead. Here is an example;

    EJBQLQuery q = new EJBQLQuery(String.format(
    "DELETE FROM %s r WHERE r.createTimestamp < :expiryTimestamp",
    Response.class.getSimpleName()));

    q.setParameter("expiryTimestamp",
    new Timestamp(System.currentTimeMillis() -
    TimeUnit.SECONDS.toMillis(expirySeconds)));

    getServerRuntime().getContext().performQuery(q);

    To help with getting Expression objects to over to EJBQL with timestamp literals;

    Expression#toEJBQL(List<Object> parameterAccumulator, String rootId)

    So you call that and as it creates the EJBQL fragment, it will insert any necessary parameters into the parameterAccumulator for you all set to go into the EJBQLQuery.

    Hopefully that helps?

    cheers.

    [1] https://github.com/aplgithub/haikudepotserver/blob/master/haikudepotserver-webapp/src/main/java/org/haikuos/haikudepotserver/captcha/DatabaseCaptchaRepository.java


    For example;

    SELECT f FROM Foo f WHERE f.createTimestamp > ?

    Then you just need to use the

    On 12/08/15 20:35, Hugi Thordarson wrote:
    Hi Andrew,
    thanks for that. I see what the problem is.
    But if I can’t use EJBQL when generating queries that use dates, can I go down any different paths or do I have to revert to writing plain old SQL?
    --
    Andrew Lindesay

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupuser @
categoriescayenne
postedAug 11, '15 at 10:09p
activeAug 12, '15 at 10:29a
posts5
users2
websitecayenne.apache.org

People

Translate

site design / logo © 2021 Grokbase