簡體   English   中英

spring-data存儲庫自定義查詢

[英]spring-data repository custom query

我正在使用帶有postgres數據庫的spring啟動應用程序,並且我在啟動時遇到異常。

例外是:

Caused by: java.lang.AbstractMethodError: org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(Ljava/lang/reflect/Method;Lorg/springframework/data/repository/core/RepositoryMetadata;Lorg/springframework/data/projection/ProjectionFactory;Lorg/springframework/data/repository/core/NamedQueries;)Lorg/springframework/data/repository/query/RepositoryQuery;
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:435) ~[spring-data-commons-1.12.1.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:220) ~[spring-data-commons-1.12.1.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:266) ~[spring-data-commons-1.12.1.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:252) ~[spring-data-commons-1.12.1.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]

我嘗試使用自定義查詢創建一個存儲庫,如下所示:

public interface LastRunRepository extends PagingAndSortingRepository<LastRun, Long> {
    LastRun findFirst1OrderByStartDateDesc();
}

我的LastRun實體看起來像:

import javax.persistence.*;
import java.util.Date;

@Entity(name = "last_run")
public class LastRun {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;

    @Column(name="start_date", nullable = false)
    private Date startDate;

    public LastRun() {
    }

    public LastRun(Date startDate) {
        this.startDate = startDate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    @Override
    public String toString() {
        return "LastRun{" +
                "id=" + id +
                ", startDate=" + startDate +
                '}';
    }
}

如果我只使用標准查詢,例如查找或保存,它就像一個魅力。 但是一旦我定義了一個自定義查詢,它就會拋出這個異常。

在mvn依賴:樹我可以看到我從sprint-boot-starter-data-jpa獲得spring-data-jpa 1.9.4版本。

+- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:1.3.3.RELEASE:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.8:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.3.3.RELEASE:compile
[INFO] |  |  \- org.apache.tomcat:tomcat-jdbc:jar:8.0.32:compile
[INFO] |  |     \- org.apache.tomcat:tomcat-juli:jar:8.0.32:compile
[INFO] |  +- org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  |  +- org.hibernate:hibernate-core:jar:4.3.11.Final:compile
[INFO] |  |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] |  |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  |  \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- javax.transaction:javax.transaction-api:jar:1.2:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:1.9.4.RELEASE:compile
+- org.springframework.data:spring-data-commons:jar:1.12.1.RELEASE:compile

我使用的是spring-data-jpa和spring-data-commons的最新版本。

所以,spring-data-commons:jar:1.12.1.RELEASE和spring-data-jpa:jar:1.9.4.RELEASE(spring-boot-starter-data-jpa:jar:1.3.3.RELEASE),不兼容?

我遇到過同樣的問題。 我確認這是由於不兼容的彈簧數據和彈簧引導依賴性。

pom.xml示例:

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
  </parent>

  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>2.0.0.RELEASE</version>
  </dependency>

在上面的pom中,Eclipse會發出警告,覆蓋管理版本1.3.4.RELEASE ,如果啟動應用程序,您將獲得所描述的異常。

您應該刪除版本並使用與spring-boot-starter-parent給出的相同的依賴:

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
  </parent>

  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
  </dependency>

你必須像這樣使用LastRun findByStartDateDesc(可分頁可分頁);

在Pageable pageable參數中,您可以將參數作為新的PageRequest(0,size)傳遞,其中size表示您想要的記錄數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM