Announcement Announcement Module
No announcement yet.
How to get named queries in hibernate-mapping to work with spring data jpa Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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);

    <query name="Thing.findNextBatchOfThings">

    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 roperty.<init>(
    at roperty.create(
    at roperty.create(
    at roperty.create(
    at roperty.create(
    at roperty.create(
    at roperty.create(
    at roperty.from(
    at art.<init>(
    at artTree$OrPart.<init>(
    at artTree.buildTree(
    at artTree.<init>(
    at TreeJpaQuery.<init>(
    at ueryLookupStrategy$CreateQueryLookupStrategy.resol veQuery(
    at ueryLookupStrategy$CreateIfNotFoundQueryLookupStra tegy.resolveQuery(
    at ueryLookupStrategy$AbstractQueryLookupStrategy.res olveQuery(
    at toryFactorySupport$QueryExecuterMethodInterceptor. <init>(
    at toryFactorySupport.getRepository(RepositoryFactory
    at toryFactoryBeanSupport.getObject(RepositoryFactory
    at toryFactoryBeanSupport.getObject(RepositoryFactory
    at eanRegistrySupport.doGetObjectFromFactoryBean(Fact
    ... 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].



    • #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


      • #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">
        select foo.stringField from Foo foo

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

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


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




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


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


              • #8
                looking forward to it.