-
May 12th, 2011, 10:54 AM
#1
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
-
May 13th, 2011, 04:19 AM
#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
-
May 13th, 2011, 08:10 AM
#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
-
May 26th, 2011, 08:24 AM
#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.
-
May 30th, 2011, 05:29 PM
#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
-
Jun 1st, 2011, 11:50 AM
#6
Thanks Ollie, I see that the snapshot releases have the fix. do you think it will be moved to milestone release soon? - cy
-
Jun 1st, 2011, 11:56 AM
#7
We will have RC1 scheduled for next week
.
-
Jun 2nd, 2011, 08:13 AM
#8
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
-
Forum Rules