Spring DataJPA custom query issue:

99封情书 提交于 2020-01-06 19:35:34

问题


I have a setup with JPA (hibernate + postgresql) and MongoDB:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.0.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.1.0.RELEASE</version>
    </dependency>

My Repository Interface is:

@Repository
public interface JpaModelRepository extends JpaRepository<ModelEntity, Integer> {
    public ModelEntity findByName(String modelNameSample);
}

My Entity:

@Entity
@Table (name = "model")
public class ModelEntity implements GenericId<Integer>, Serializable {

    ....
    @Column (name = "name_tx")
    private String name;

Implementation of the repository interface is auto-generated by SpringData. If I remove the findByName, all works fine in my project. If I leave it I have this in tomcat error log:

Caused by: java.lang.NoSuchMethodError: org.springframework.data.repository.query.parser.Part.getProperty()Lorg/springframework/data/repository/query/parser/Property;
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:163)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:95)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:49)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:102)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:59)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:93)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:164)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:71)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:269)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:142)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:114)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:38)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    ... 85 more

Tried to use this documentation but failed for some reason: Spring Doc

Hope is one of you guys managed to create a custom query (like findBySomeProperty(...)) ...

Thank you


回答1:


The Spring data APIs changed bewteen 1.0 and 1.1 releases, that is why this error. Use the same version for both of your dependencies and it should work.




回答2:


I had the same problem of

org.springframework.data.repository.query.parser.PartTree.isCountProjection()Ljava/lang/Boolean;

But I resolved it by using a proper combination of jars like

spring-data-jpa-1.2.0.RELEASE.jar <BR>
spring-data-commons-1.6.3.RELEASE.jar <BR>
spring-data-commons-core-1.4.1.RELEASE.jar<BR>
hibernate-entitymanager-4.1.12.final.jar<BR>
hibernate-jpa-2.1-api-1.0.0.final.jar<BR>
hibernate-validator-4.2.0.final.jar<BR>
hibernate-commons-annotations-4.0.0.CR2.jar<BR>

and using Spring core 3.2.5 version of jars. The problem was solved.

I was also using a proper dialect, i.e. the right driver to connect to the database.




回答3:


I found the answer:

    <spring.version>3.1.2.RELEASE</spring.version>
    <spring-data.jpa.version>1.2.0.BUILD-SNAPSHOT</spring-data.jpa.version>
    <spring-data.mongodb.version>1.1.0.BUILD-SNAPSHOT</spring-data.mongodb.version>

https://github.com/SpringSource/spring-data-multistore-test

I did some maven test combining version on that git repository




回答4:


I got the same problem when trying to use Spring Data JPA + Hibernate + Spring Framework 4.0. The problem is that the Spring Data project doesn't keep-up at the same rate as the Spring Framework project.

I resolve the problem using the proper JAR version combination and excluding any other Spring project, so that those depended JAR could be downloaded correctly and automatically based on the "Spring Data" pom.xml file. Check here all the Spring Data dependencies: http://search.maven.org/#artifactdetails%7Corg.springframework.data%7Cspring-data-jpa%7C1.6.0.RELEASE%7Cjar

I changed my maven pom.xml file to:

    <properties>

    <!-- Generic properties -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <!-- Spring -->
    <spring-framework.version>3.2.9.RELEASE</spring-framework.version>
    <spring-data.version>1.6.0.RELEASE</spring-data.version>

    <!-- Hibernate / JPA -->
    <hibernate.version>3.6.10.Final</hibernate.version>

    <!-- Test -->
    <junit.version>4.11</junit.version>

    </properties>

    <dependencies>

    <!-- Spring framework -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${spring-data.version}</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>${hibernate-jpa.version}</version>
    </dependency>

            ....
</dependencies>


来源:https://stackoverflow.com/questions/12529268/spring-datajpa-custom-query-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!