Results 1 to 8 of 8

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

  1. #1
    Join Date
    May 2011
    Posts
    14

    Default 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. #2
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    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

  3. #3
    Join Date
    May 2011
    Posts
    14

    Default

    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

  4. #4
    Join Date
    May 2011
    Posts
    14

    Default

    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.

  5. #5
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    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

  6. #6
    Join Date
    May 2011
    Posts
    14

    Default

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

  7. #7
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    We will have RC1 scheduled for next week .

  8. #8
    Join Date
    May 2011
    Posts
    14

    Default

    looking forward to it.

Tags for this Thread

Posting Permissions

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