【问题标题】:Spring Data Solr : Unable to locate Attribute from RepositorySpring Data Solr:无法从存储库中找到属性
【发布时间】:2015-07-31 17:37:13
【问题描述】:

在我设法迁移到 Spring Boot 之前,这一切正常。

在我使用 spring-data-solr 版本 : 1.4.0.RELEASE 之前,现在 Boot 刚刚获得版本 1.3.3

我试图删除 spring-boot-starter-data-solr 并重置为静态定义,但我遇到了同样的问题..

更新我当前的pom.xml

 <project>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.2.5.RELEASE</version>
        </parent>
 <repositories>
        <repository>
            <id>excilys-release</id>
            <url>http://repository.excilys.com/content/repositories/releases</url>
        </repository>

        <repository>
            <id>repo2_maven_org</id>
            <url>http://repo2.maven.org/maven2</url>
        </repository>

        <repository>
            <id>maven-restlet</id>
            <url>http://maven.restlet.com</url>
        </repository>
    </repositories>
    <dependencies>

            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-websocket</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-solr</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>

            <!-- Spring Solr -->
            <dependency>
                <groupId>org.apache.solr</groupId>
                <artifactId>solr-core</artifactId>
                <version>4.10.0</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-jdk14</artifactId>
                    </exclusion>
                    <exclusion>
                        <artifactId>log4j</artifactId>
                        <groupId>log4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
    </project>

我目前的SolrConfig.java

@Configuration
@EnableSolrRepositories(basePackages = "com.repository.solr")
@ComponentScan("com.repository.solr")
public class SolRConfig {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public static final String PROPERTY_SOLR_SERVER_CORE  = "solr.core";

    @Resource
    private Environment environment;
    @Resource OgcParameters ogcParameters;

    @Bean
    public SolrServer solrServer() {
        final OgcParameters.slr slr = ogcParameters.slr();
        if (!environment.containsProperty(PROPERTY_SOLR_SERVER_CORE)) {
            logger.warn("You are running on the default solr core, create or edit the application.properties to avoid this behaviour");
        }

        String solrHostWithCoreIfNecessary = slr.host()
                .concat(slr.core());

        return new HttpSolrServer(solrHostWithCoreIfNecessary);
    }

    @Bean
    public SolrOperations solrTemplate() throws ParserConfigurationException, SAXException, IOException {
        return new SolrTemplate(solrServer());
    }
}

我目前的SolrRepository.java

@Repository
public interface DocumentSolrRepository extends SolrCrudRepository<Document, Long>, DocumentSolrRepositoryCustom {
    Set<Document> findByTextContaining(@Param("text") String test);
}

我当前的 entity 使用绑定到 SolrRepository 字段的@Field 属性导致服务器启动失败:

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Document {

    private Long id;
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="DOCUMENT_SEQ")
    @SequenceGenerator(name="DOCUMENT_SEQ", sequenceName="DOCUMENT_SEQ")
    public Long getId() {
        return id;
    }
    public Document setId(Long id) {
        this.id = id;
        return this;
    }
    /* SOLR Specific Fields */
    @Field("text")
    private String text;
    @Transient @JsonIgnore
    public String getText() {
        return text;
    }
    public Document setText(String text) {
        this.text = text;
        return this;
    }
}

错误记录堆栈跟踪

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicantController': Injection of resource dependencies failed;

Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [text] on this ManagedType [com.entity.Document]
    at org.hibernate.jpa.internal.metamodel.AbstractManagedType.checkNotNull(AbstractManagedType.java:144)
    at org.hibernate.jpa.internal.metamodel.AbstractManagedType.getAttribute(AbstractManagedType.java:130)
    at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:475)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:199)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:146)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:86)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:44)
    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:110)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:230)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:64)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:94)
    ... 68 more

【问题讨论】:

    标签: java spring solr spring-boot spring-data-solr


    【解决方案1】:

    您不需要排除或添加任何其他依赖项。相反,您可以使用以下属性简单地定义 spring-data 项目要使用的版本:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
    </parent>
    
    <properties>
        <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
    </properties>
    
    <dependencies>
        ...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-solr</artifactId>
        </dependency>
        ...
    </dependencies>
    

    不需要其他任何东西。顺便说一句,Fowler-SR2 是 28.07 发布的(它仍然很热)。

    【讨论】:

    • 定义这个属性并没有解决我的问题。我在 pom.xml 的帖子中添加了额外的元素(存储库定义)。我目前需要 Spring Solr 额外的依赖项,所以我不能仅使用 Spring boot 数据 solr 包..
    • 这个实体是否打算使用 JPA 和 Solr 存储? (文档似乎也是 JPA Entity
    • 另外,@Repository 不是必需的,DocumentSolrRepository 通过扩展SolrCrudRepository 就足够了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 2018-09-15
    • 2017-09-19
    • 1970-01-01
    相关资源
    最近更新 更多