Announcement Announcement Module
Collapse
No announcement yet.
Hibernate: namedQuery and Criteria Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate: namedQuery and Criteria

    Hi all

    In my app I defined a couple of queries in a hbm.xml files, to simplify handling the data via GenericDao.
    Now I want to use Example queries.

    is there a way to *marry* getNamedQuery() with Criteria queries in Hibernate3, so that one can apply additional constraints on an already defined query on the fly?

    I tried also to use filters, but as soon as they are gobal session-wide objects, I had to manually enable/disable each of them. That also could lead to some weird consequences if one would use OSIV...

    So, is there a nice way to handle that?

  • #2
    Originally posted by Injecteer View Post
    Hi all

    In my app I defined a couple of queries in a hbm.xml files, to simplify handling the data via GenericDao.
    Now I want to use Example queries.

    is there a way to *marry* getNamedQuery() with Criteria queries in Hibernate3, so that one can apply additional constraints on an already defined query on the fly?

    I tried also to use filters, but as soon as they are gobal session-wide objects, I had to manually enable/disable each of them. That also could lead to some weird consequences if one would use OSIV...

    So, is there a nice way to handle that?
    As far as I know you have no chances to marry them. Build your query yourselves (or store them in DB). You as will may consider using not SQL queries, but HQL. In this case you need nothing more then query text itself.

    Comment


    • #3
      I'm usling HQL of course for the most part. Having to modify queries' texts is not really OO for me .
      So, hibernate suxx.
      What really strange is, that when a named query gets executed, it's parsed, parametrized and converted into a dialect-based SQL string. Just before the last step the query is represented as a Java object -- a Query. That means, you should be able to hook-in anything you want, but you can't.

      btw, maybe somebody can point to a reference or example on how to build criteria queries (from HQL) ? I tried to look at 1 page of text concerning the subject in hibernate reference, as well as in "Hibernate in Action", but there only some realy basic cat-kittens-based examples are given.

      Comment


      • #4
        OO is (or, at least, should be) only a tool, not a goal. Goal is to simplify the application and to make it more managable. As soon as you can reach this goal by manipulating query text - it is absolutely ok. Anyway, net result is query text manipulationg - as at the end Hibernate sends to DB exactly manipulated query text. Who direct (or how indirect) is this manipulation is another story.

        Can you provide a realistic sample of a query and additional constraints for it?

        Concerning Hibernate, as far as I know Criteria API, HQL query (Query) and SQL (SQLQuery) are completely unrelated and there is no way to convert oné to the another.

        If you really want to work in OO-way only (or mostly) you have to use Criteria API whenever possible, but it is more limited, then ever HQL.

        Some examples you may found in the Hibernate test suite (test subdirectory in Hibernate installations), especially, in test\org\hibernate\test\criteria\* and test\org\hibernate\test\legacy\QueryByExampleTest. java.

        But, IMHO, this discussion rather belongs to Hibernate and not Spring forums, as it has nothing to do with Spring.

        Regards,
        Oleksandr

        Originally posted by Injecteer View Post
        I'm usling HQL of course for the most part. Having to modify queries' texts is not really OO for me .
        So, hibernate suxx.
        What really strange is, that when a named query gets executed, it's parsed, parametrized and converted into a dialect-based SQL string. Just before the last step the query is represented as a Java object -- a Query. That means, you should be able to hook-in anything you want, but you can't.

        btw, maybe somebody can point to a reference or example on how to build criteria queries (from HQL) ? I tried to look at 1 page of text concerning the subject in hibernate reference, as well as in "Hibernate in Action", but there only some realy basic cat-kittens-based examples are given.

        Comment

        Working...
        X