“没有找到类型的属性”......将 QueryDslPredicateExecutor 与 MongoDB 和 Spring-Data 一起使用时

Posted

技术标签:

【中文标题】“没有找到类型的属性”......将 QueryDslPredicateExecutor 与 MongoDB 和 Spring-Data 一起使用时【英文标题】:"No property exists found for type"... When using the QueryDslPredicateExecutor with MongoDB and Spring-Data 【发布时间】:2016-10-13 09:03:40 【问题描述】:

我正在尝试将 QueryDslPredicateExecutor 与 MongoDB 和 Spring-Data 一起使用,但它似乎对“exists()”属性感到窒息。

我正在使用 -

org.springframework.boot:spring-boot-starter-parent:1.3.5.RELEASE  
com.querydsl:querydsl-mongodb:4.1.2  
com.querydsl:querydsl-apt:4.1.2  
org.mongodb.morphia:morphia:1.1.1  

堆栈跟踪

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property exists found for type Tree!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:84) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.mongodb.repository.query.PartTreeMongoQuery.<init>(PartTreeMongoQuery.java:60) ~[spring-data-mongodb-1.9.0.RELEASE.jar:na]
    at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory$MongoQueryLookupStrategy.resolveQuery(MongoRepositoryFactory.java:168) ~[spring-data-mongodb-1.9.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:435) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:220) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:266) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:252) ~[spring-data-commons-1.12.0.RELEASE.jar:na]
    at org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean.afterPropertiesSet(MongoRepositoryFactoryBean.java:108) ~[spring-data-mongodb-1.9.0.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    ... 30 common frames omitted

这个类正在抛出异常

package org.springframework.data.mapping
// PropertyPath.class 
....

/**
 * Creates a leaf @link PropertyPath (no nested ones with the given name and owning type.
 * 
 * @param name must not be @literal null or empty.
 * @param owningType must not be @literal null.
 * @param base the @link PropertyPath previously found.
 */
PropertyPath(String name, TypeInformation<?> owningType, List<PropertyPath> base) 

    Assert.hasText(name, "Name must not be null or empty!");
    Assert.notNull(owningType, "Owning type must not be null!");
    Assert.notNull(base, "Perviously found properties must not be null!");

    String propertyName = name.matches(ALL_UPPERCASE) ? name : StringUtils.uncapitalize(name);
    TypeInformation<?> propertyType = owningType.getProperty(propertyName);

    if (propertyType == null) 
        throw new PropertyReferenceException(propertyName, owningType, base);
    

    this.owningType = owningType;
    this.isCollection = propertyType.isCollectionLike();
    this.type = propertyType.getActualType();
    this.name = propertyName;

属性(方法)来自我的存储库扩展的 QueryDslPredicateExecutor 类。

public abstract boolean org.springframework.data.querydsl.QueryDslPredicateExecutor.exists(com.querydsl.core.types.Predicate)  

这里是存储库-

public interface TreeRepository extends ExtendedMongoRepository<Tree, String>, QueryDslPredicateExecutor<Tree>

【问题讨论】:

这似乎是由于存储库直接扩展了基本接口而不是 MongoRepository - 此处已发布类似问题 - ***.com/questions/36293960/… 【参考方案1】:

我最终通过扩展和实现 QueryDslPredicateExecutor 而不是更高级别的存储库来解决这个问题。

// Custom repository interface
@NoRepositoryBean
public interface ExtendedMongoRepository<T, ID extends Serializable> extends MongoRepository<T, ID>, QueryDslPredicateExecutor<T>

  public Page<T> query(Query query, Pageable pageable);




// Custom Repository Implementation
public abstract class ExtendedMongoRepositoryImpl<T, ID extends Serializable> extends QueryDslMongoRepository<T, ID>
        implements ExtendedMongoRepository<T, ID> 

    private Class<T> clazz;
    private MongoOperations mongoOperations;
    @SuppressWarnings("unused")
    private MongoEntityInformation<T, ID> metadata;

    public ExtendedMongoRepositoryImpl(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) 
        super(metadata, mongoOperations);
        this.mongoOperations = mongoOperations;
        this.clazz = metadata.getJavaType();
        this.metadata = metadata;
    

    @Override
    public Page<T> query(Query query, Pageable pageable) 
        List<T> list =  mongoOperations.find(query.with(pageable), clazz);
        return new PageImpl<T>(list, pageable, list.size());
    
  

// Entity Repository Interface
public interface TreeRepository extends ExtendedMongoRepository<Tree, String> 

【讨论】:

那个公共类不是抽象的? 它在我的应用程序中,我只是忘记更新我的解决方案。谢谢! ***.com/questions/38153365/… 也许对此有任何想法?【参考方案2】:

变化

extends SimpleJpaRepository

extends QueryDslJpaRepository

在我的基础 JPA 存储库中,实现了它。

【讨论】:

谢谢。您的回答帮助我理解了这个问题。【参考方案3】:

@laffuste 帮助我理解了这个问题。如果您声明 baseRepositoryClass(或扩展它),它与 Querydsl 不兼容。您需要将其更改为 Querydsl 基类,或者对其进行扩展,这两者都兼容。

public class SimpleSpecRepository<T, ID extends Serializable> extends QuerydslJpaRepository<T, ID>;

@EnableJpaRepositories(value = "com.foo.repository", repositoryBaseClass = SimpleSpecRepository.class)
    @SpringBootApplication
    public class ServiceEntryPoint 

注意:它要求所有存储库接口的实体映射都有Q映射。如果你不想这样,你需要创建一个单独的repositoryFactoryBeanClass:

public class SimpleSpecRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>;

@EnableJpaRepositories(value = "com.foo.repository", repositoryFactoryBeanClass = SimpleRepositoryFactoryBean.class)
@SpringBootApplication
public class ServiceEntryPoint 

 /**
 * Extension of the @link JpaRepositoryFactoryBean interface.
 *
 * @param <T>  the type of the repository
 * @param <S>  the type of the entity
 * @param <ID> the type of the entity identifier
 */
public class SimpleRepositoryFactoryBean<T extends JpaRepository<S, ID>, S, ID extends Serializable>
        extends JpaRepositoryFactoryBean<T, S, ID> 

    /**
     * Creates a new @link SimpleRepositoryFactoryBean for the given repository
     * interface.
     *
     * @param repositoryInterface must not be @literal null.
     */
    public SimpleRepositoryFactoryBean(Class<? extends T> repositoryInterface) 
        super(repositoryInterface);
    

    @Override
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) 
        return new SimpleJpaRepositoryFactory<S, ID>(entityManager);
    

    /**
     * Extends the JPA specific generic repository factory.
     * 
     * @param <S>  the type of the entity
     * @param <ID> the type of the entity identifier
     */
    private static class SimpleJpaRepositoryFactory<S, ID extends Serializable> extends JpaRepositoryFactory 

        /**
         * Creates a new @link SimpleJpaRepositoryFactory.
         * 
         * @param entityManager must not be @literal null
         */
        public SimpleJpaRepositoryFactory(EntityManager entityManager) 
            super(entityManager);
        

        @Override
        protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) 

            if (isQueryDslExecutor(metadata.getRepositoryInterface())) 
                return QuerydslJpaRepository.class;
             else 
                return SimpleSpecRepository.class;
            
        

        /**
         * Returns whether the given repository interface requires a QueryDsl specific
         * implementation to be chosen.
         * 
         * @param repositoryInterface
         * @return
         */
        private boolean isQueryDslExecutor(Class<?> repositoryInterface) 

            return QUERY_DSL_PRESENT && QuerydslPredicateExecutor.class.isAssignableFrom(repositoryInterface);
        

    


注意:这仅适用于复杂的配置。如果你有一个简单的配置,你应该能够做到以下几点:

@EnableJpaRepositories("com.foo.repository")

默认配置应自动支持 Querydsl 或 SimpleJPA。

【讨论】:

以上是关于“没有找到类型的属性”......将 QueryDslPredicateExecutor 与 MongoDB 和 Spring-Data 一起使用时的主要内容,如果未能解决你的问题,请参考以下文章

drf序列化器的实例

Cloudkit:CKDatabaseOperation 不适用于蜂窝网络

XMLHttpRequest下载文件,js下载文件,支持后台消息在前端页面提示

将自己的博客园,打造成个人知乎

如何将thinkcmf导入eclipse

如何将Ios文件上传到