2014-06-15 13 views
5

Utilizzo Spring JPA con Hibernate & PostgreSQL.Il passaggio da @Query a nativeQuery causa PropertyReferenceException

Ho il seguente repository JPA:

import org.springframework.data.domain.Page; 
import org.springframework.data.domain.Pageable; 
import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.data.jpa.repository.Query; 
import org.springframework.data.repository.query.Param; 

import java.util.UUID; 

public interface EventRepository extends JpaRepository<Event, UUID> { 
    @Query(value = "Select * From event Where ST_Intersects(ST_SetSRID(ST_MakeBox2D(ST_MakePoint(:swLongitude, :swLatitude), ST_MakePoint(:neLongitude, :neLatitude)), 4326), location)", nativeQuery = true) 
    Page<Event> qwerty(@Param("swLatitude") double swLatitude, @Param("swLongitude") double swLongitude, @Param("neLatitude") double neLatitude, @Param("neLongitude") double neLongitude, Pageable pageable); 
} 

Ai tempi in cui la query era HQL e non ha avuto nativeQuery insieme come true ha funzionato bene. Ora ho bisogno di passare a una query SQL nativa, e anche se l'aggiunta di nativeQuery = true e la riscrittura della query lo risolverebbero.

Tuttavia, io ora ottenere:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property qwerty found for type Event! 
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75) 
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) 
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307) 
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270) 
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241) 
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) 
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:213) 
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:321) 
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:301) 
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:82) 
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:60) 
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:91) 
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:168) 
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69) 
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:320) 
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:169) 
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224) 
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210) 
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) 
... 34 more 

Ovviamente, è usato per non essere chiamati qwerty; L'ho appena rinominato per illustrare meglio questo.

Sembra ignorare l'annotazione @Query in qualche modo e che è l'annotazione che dovrebbe definire la query da eseguire e tenta invece di interpretarla in base al nome del metodo.

Qualche idea su cosa sto facendo male?

risposta

11

recuperate dalla documentazione di primavera dati JPA (versione 1.6.0.RELEASE):

L'annotazione @ query consente di eseguire le query native impostando il flag nativeQuery su true. Nota che al momento non supportiamo l'esecuzione dell'impaginazione o l'ordinamento dinamico per le query native, poiché avremmo necessario manipolare la query effettiva dichiarata e non possiamo farlo in modo affidabile per SQL nativo .

È evidente che le query native non funzioneranno con l'impaginazione.

Quindi, se avete assolutamente bisogno di supporto di query nativo si dovrà abbandonare l'impaginazione, o avrete a destra un'implementazione repository personalizzati in cui si sarà attuare tale funzione sul tuo

+1

Grazie per la risposta rapida e corretta , Avrei dovuto leggere più attentamente. Il messaggio di errore avrebbe potuto essere più user-friendly però. –

+0

@JohnM Benvenuto! – geoand

+3

@geo e FYI Abbiamo creato https://jira.spring.io/browse/DATAJPA-554 per creare un messaggio di eccezione più sano in casi come questo. –