Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-data repository custom query

I am using a spring boot application with a postgres database, and i do get an exception at the startup time.

The exception is:

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]

I tried to create a repository with a custom query, which looks like:

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

My LastRun Entity looks like:

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 +
                '}';
    }
}

If i am using just the standard queries, like find or save, it works like a charm. But as soon as i define a custom query it throws me this exception.

In mvn dependency:tree i can see that i get the spring-data-jpa 1.9.4 version from the sprint-boot-starter-data-jpa.

+- 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

I am using the latest versions of spring-data-jpa and spring-data-commons.

So, spring-data-commons:jar:1.12.1.RELEASE and spring-data-jpa:jar:1.9.4.RELEASE (spring-boot-starter-data-jpa:jar:1.3.3.RELEASE), are not compatible?

like image 950
krackmoe Avatar asked Dec 02 '25 17:12

krackmoe


1 Answers

I had the same issue. I confirm this was due to incompatible spring data and spring boot dependencies.

Example of 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>

In above pom, Eclipse raise a warning, overriding manage version 1.3.4.RELEASE and if you start your app you will get the described exception.

You should remove the version and use the same dependecies as the one given in 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>
like image 74
L. G. Avatar answered Dec 04 '25 08:12

L. G.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!