Announcement Announcement Module
Collapse
No announcement yet.
How to get named queries in hibernate-mapping to work with spring data jpa Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get named queries in hibernate-mapping to work with spring data jpa

    I have a query in hibernate-mapping file like the following. and I have my ADO repository defined as this:
    public interface ThingDao extends JpaRepository<Thing, String> {

    List<String> findNextBatchOfThings(String lastThingId);
    }

    <hibernate-mapping>
    <query name="Thing.findNextBatchOfThings">
    <![CDATA[
    .....
    ]]>
    </query>
    ....

    I got an exception like the following. I'm sure it is related to the named query, but I
    didn't find in the doc how to do it. Many thanks for any pointers!!


    Caused by: java.lang.IllegalArgumentException: No property find found for type class java.lang.String
    at org.springframework.data.repository.query.parser.P roperty.<init>(Property.java:66)
    at org.springframework.data.repository.query.parser.P roperty.create(Property.java:270)
    at org.springframework.data.repository.query.parser.P roperty.create(Property.java:288)
    at org.springframework.data.repository.query.parser.P roperty.create(Property.java:288)
    at org.springframework.data.repository.query.parser.P roperty.create(Property.java:288)
    at org.springframework.data.repository.query.parser.P roperty.create(Property.java:288)
    at org.springframework.data.repository.query.parser.P roperty.create(Property.java:250)
    at org.springframework.data.repository.query.parser.P roperty.from(Property.java:209)
    at org.springframework.data.repository.query.parser.P art.<init>(Part.java:48)
    at org.springframework.data.repository.query.parser.P artTree$OrPart.<init>(PartTree.java:242)
    at org.springframework.data.repository.query.parser.P artTree.buildTree(PartTree.java:101)
    at org.springframework.data.repository.query.parser.P artTree.<init>(PartTree.java:77)
    at org.springframework.data.jpa.repository.query.Part TreeJpaQuery.<init>(PartTreeJpaQuery.java:51)
    at org.springframework.data.jpa.repository.query.JpaQ ueryLookupStrategy$CreateQueryLookupStrategy.resol veQuery(JpaQueryLookupStrategy.java:100)
    at org.springframework.data.jpa.repository.query.JpaQ ueryLookupStrategy$CreateIfNotFoundQueryLookupStra tegy.resolveQuery(JpaQueryLookupStrategy.java:176)
    at org.springframework.data.jpa.repository.query.JpaQ ueryLookupStrategy$AbstractQueryLookupStrategy.res olveQuery(JpaQueryLookupStrategy.java:73)
    at org.springframework.data.repository.support.Reposi toryFactorySupport$QueryExecuterMethodInterceptor. <init>(RepositoryFactorySupport.java:259)
    at org.springframework.data.repository.support.Reposi toryFactorySupport.getRepository(RepositoryFactory Support.java:143)
    at org.springframework.data.repository.support.Reposi toryFactoryBeanSupport.getObject(RepositoryFactory BeanSupport.java:107)
    at org.springframework.data.repository.support.Reposi toryFactoryBeanSupport.getObject(RepositoryFactory BeanSupport.java:36)
    at org.springframework.beans.factory.support.FactoryB eanRegistrySupport.doGetObjectFromFactoryBean(Fact oryBeanRegistrySupport.java:142)
    ... 40 more

  • #2
    Spring Data JPA does *not* detect hibernate queries. You have to use JPA named queries [1] (either via annotation or declared in orm.xml). As we don't find a NamedQuery or one annoted using @Query we start deriving the query from the method name. Thus you see it failing to find a property. You can enforce Spring Data JPA to only use manually declared queries by setting query-lookup-strategy to USE_DECLARED_QUERY [2].

    [1] http://static.springsource.org/sprin....named-queries
    [2] http://static.springsource.org/sprin...kup-strategies

    Comment


    • #3
      Thank Oliver. That's what I thought. I may have to look back on this later, for the moment I will use what's available from spring data. I'm not sure if there is a plan to add hibernate query support? - sorry for asking

      Comment


      • #4
        I have switched to use jpa orm.xml for named queries, but here is a new problem: whenever the query returns String or something other than the object itself it stops working. So for example, if I define the query as this:
        <named-query name="Foo.testQuery">
        <query>
        select foo.stringField from Foo foo
        </query>
        </named-query>

        and the FooRepository.java has this:
        List<String> testQuery();

        spring will have error of not finding the query!
        thanks for any feedback! I will really appreciate it.

        Comment


        • #5
          It seems you're suffering from this one [1]. So that should be working if you use a latest snapshot actually.

          Cheers,
          Ollie

          [1] https://jira.springsource.org/browse/DATAJPA-44

          Comment


          • #6
            Thanks Ollie, I see that the snapshot releases have the fix. do you think it will be moved to milestone release soon? - cy

            Comment


            • #7
              We will have RC1 scheduled for next week .

              Comment


              • #8
                looking forward to it.

                Comment

                Working...
                X