Results 1 to 8 of 8

Thread: How to manually define a named query for JPA

  1. #1
    Join Date
    Jul 2005
    Location
    Munich, Germany
    Posts
    153

    Default How to manually define a named query for JPA

    Hello,

    I'm looking for a way to define a named query manually (programmatically) in a JPA environment but couldn't find any API for doing this.

    Any ideas?


    Oliver

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I'm confused, isn't this just a find?
    http://www.springframework.org/docs/... g.Object...)

  3. #3
    Join Date
    Jul 2005
    Location
    Munich, Germany
    Posts
    153

    Default

    Quote Originally Posted by karldmoore View Post
    I'm confused, isn't this just a find?
    http://www.springframework.org/docs/... g.Object...)
    No, what I want to do is:

    Code:
    EntityManager entityManager = ...;
    
    entityManager.defineNamedQuery("myQuery", "select * from Foo"); // This is the imaginary method I'm looking for
    
    List l = entityManager.createNamedQuery("myQuery").getResultList();

    Oliver

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I'm not sure if you can do that. If you find something that says you can, feel free to share . I'm interested, why you need to specify these dynamically anyway?

  5. #5
    Join Date
    Jul 2005
    Location
    Munich, Germany
    Posts
    153

    Default

    Quote Originally Posted by karldmoore View Post
    I'm not sure if you can do that. If you find something that says you can, feel free to share . I'm interested, why you need to specify these dynamically anyway?
    I need this only for cosmetics :-)

    I have a GenericDAO that builds some JQL statements for later execution. I'd like to bind them to a name to be able to get the statement by name.

    Another small benefit would probably be, that the statement would get prepared (and syntax would be checked) before it is used the first time, so I would know all errors on application startup.


    Oliver

  6. #6

    Default

    I have a GenericDAO that builds some JPQL statements for later execution. I'd like to bind them to a name to be able to get the statement by name.
    There is no such API. Manage them yourself in a HashMap.
    Another small benefit would probably be, that the statement would get prepared (and syntax would be checked) before it is used the first time, so I would know all errors on application startup.
    The JPA spec says nothing about an implementation "preparing" a statement. A PreparedStatement is attached to a Connection, which isnt known til you have a transaction and you invoke it. No syntax checking is required until it is invoked either. There is no benefit to be had.
    -Andy
    DataNucleus - Standardised persistence, multiple datastores

  7. #7
    Join Date
    Jul 2005
    Location
    Munich, Germany
    Posts
    153

    Default

    Quote Originally Posted by andy View Post
    The JPA spec says nothing about an implementation "preparing" a statement. A PreparedStatement is attached to a Connection, which isnt known til you have a transaction and you invoke it. No syntax checking is required until it is invoked either. There is no benefit to be had.
    When I said that the statement would get prepared, I didn't mean a PreparedStatement in the sense of a database PREPARE. I'm using Hibernate EntityManager for JPA support - all statements in my orm.xml are being validated on startup, so If there's an error I know it on startup. If you have changed many things in your Domain Layer, you're lucky if you get all errors in one step and do not have to check all possible places.

  8. #8
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Obviously executing the query would be one way of testing it . You could run them all at startup. I guess the other way would be to find the code that does the query syntax validation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •