使用Join定义JPA存储库本机查询

时间:2015-12-08 11:07:20

标签: java sql spring jpa join

我有一个带有Class Ride的Spring项目

Entity
@Table(name = "ride")
public class Ride {
    @Id
    @GeneratedValue
    private Long id;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="start_date")
    private Date startDate;
    @Temporal(TemporalType.TIMESTAMP)
    private Date endDate;
    private int nbPlaces;
    private int price;
    public int rank;
    @Column(name="departure_country")
    private String departureCountry;
    @Column(name="departure_city")
    private String departureCity;
    @Column(name="arrival_country")
    private String arrivalCountry;
    @Column(name="arrival_city")
    private String arrivalCity;
    private String appointmentAddress;
    @ManyToOne(optional = false)
    @JsonIgnore
    private Travel travel;
    <.....>    
}

当我使用本机sql查询创建存储库时

public interface RideRepository extends JpaRepository<Ride,Long> {

    @Query(value = "SELECT departureQuery.travel FROM (SELECT DISTINCT travel FROM ride WHERE departure_city LIKE %?0% AND start_date > ?2 OR departure_country LIKE %?0% AND start_date > ?2) departureQuery JOIN (SELECT DISTINCT travel FROM ride  WHERE arrival_city LIKE %?1% AND start_date > ?2 OR arrival_country LIKE %?1% AND start_date > ?2) arrivalQuery on departureQuery.travel = arrivalQuery.travel", nativeQuery = true)
    List<Long> find(String departureCity, String arrivalCity, String date);

} 
  • 第一个FROM加下划线:标识符预期,得到'('
  • 第二个SELECT带有下划线:'SELECT'意外

当我尝试部署war文件时,我获得了HTTP Status 500,

java.lang.IllegalArgumentException: Position must be greater than zero!
    org.springframework.util.Assert.isTrue(Assert.java:65)
    org.springframework.data.jpa.repository.query.StringQuery$LikeParameterBinding.<init>(StringQuery.java:622)
    org.springframework.data.jpa.repository.query.StringQuery$ParameterBindingParser.parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(StringQuery.java:241)
    org.springframework.data.jpa.repository.query.StringQuery$ParameterBindingParser.access$000(StringQuery.java:141)
    org.springframework.data.jpa.repository.query.StringQuery.<init>(StringQuery.java:59)
    org.springframework.data.jpa.repository.query.ExpressionBasedStringQuery.<init>(ExpressionBasedStringQuery.java:59)
    org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.<init>(AbstractStringBasedJpaQuery.java:61)
    org.springframework.data.jpa.repository.query.NativeJpaQuery.<init>(NativeJpaQuery.java:46)
    org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72)
    org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:53)
    org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:135)
    org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:203)
    org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:72)
    org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:349)
    org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:187)
    org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
    org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225)

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rideRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Position must be greater than zero!
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1554)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1021)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:964)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:494)
    org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

索引从1开始查询参数而不是0。 改变&#34; LIKE%?0%&#34;到&#34; LIKE%?1%&#34; 相应地更改其他参数索引