Spring Data JPA自定义查询问题:

时间:2012-09-21 11:03:36

标签: java jpa spring-data

我有一个JPA(hibernate + postgresql)和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>

我的存储库界面是:

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

我的实体:

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

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

存储库接口的实现由SpringData自动生成。如果我删除了findByName,那么在我的项目中一切正常。如果我离开它,我在tomcat错误日志中有这个:

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

尝试使用此文档但由于某些原因失败: Spring Doc

希望是你们其中一个设法创建自定义查询(比如findBySomeProperty(...))......

谢谢

4 个答案:

答案 0 :(得分:1)

Spring数据API在1.0和1.1版本之间发生了变化,这就是为什么会出现这个错误。对两个依赖项使用相同的版本,它应该可以工作。

答案 1 :(得分:1)

我有同样的问题

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

但我通过使用适当的罐子组合来解决它,比如

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>

并使用Spring核心3.2.5版本的jar。问题解决了。

我还使用了正确的方言,即连接数据库的正确驱动程序。

答案 2 :(得分:0)

我找到了答案:

    <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

我在git存储库上做了一些maven测试组合版本

答案 3 :(得分:0)

我在尝试使用Spring Data JPA + Hibernate + Spring Framework 4.0时遇到了同样的问题。问题是Spring Data项目的运行速度与Spring Framework项目相同。

我使用正确的JAR版本组合解决问题并排除任何其他Spring项目,以便可以根据&#34; Spring Data&#34;正确地自动下载这些依赖的JAR。 pom.xml文件。在这里检查所有Spring Data依赖项:http://search.maven.org/#artifactdetails%7Corg.springframework.data%7Cspring-data-jpa%7C1.6.0.RELEASE%7Cjar

我将maven pom.xml文件更改为:

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