草庐IT

spring - org.springframework.data.mapping.PropertyReferenceException : No property catch found for type

coder 2023-05-10 原文

我对 Spring Data 存储库有疑问。

当我部署时,我得到一个异常,这是因为 Spring Data 尝试动态派生该方法,但在 Entity 中找不到相应的属性。

如何将自定义方法放入自定义存储库中而不出现此问题?

这些是涉及的组件:

  1. LocaleJpaImpl : 实体
  2. LocaleJpaRepositoryClient :业务层类
  3. interface LocaleJpaRepository extends JpaRepository<LocaleJpaImpl, Long>, LocaleJpaRepositoryCustom
  4. interface LocaleJpaRepositoryCustom
  5. LocaleJpaRepositoryImplemented implements LocaleJpaRepositoryCustom

LocaleJpaRepositoryCustom有一个方法:

List<String> catchLanguagesCombinations() throws DAOSystemException;

(LanguagesCombinations 不是 LocaleJpaImpl 的属性。因为该动机在自定义存储库中)。

这个异常(exception):

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property languages found for type com.engine.i18n.domain.LocaleJpaImpl
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:74)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:326)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:352)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:306)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:244)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:73)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:180)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:260)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:240)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:68)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:57)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:280)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:148)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 33 more

这是相关代码:

1. LocaleJpaImpl:

import java.io.Serializable;
import javax.persistence.AttributeOverride;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import com.jpa.BaseEntityJpaSupport;

@Entity
@Table(name = "LOCALE")
@XmlRootElement
@AttributeOverride(name="id", column=@Column(name="LOCALE_ID"))

@NamedQueries({
    @NamedQuery(name = "Locale.findAll", query = "FROM LocaleJpaImpl l"),
    @NamedQuery(name = "Locale.findByLocaleId", query = "FROM LocaleJpaImpl l WHERE l.localeId = :localeId"),
    @NamedQuery(name = "Locale.findByLanguageCode", query = "FROM LocaleJpaImpl l WHERE l.languageCode = :languageCode")

public class LocaleJpaImpl extends BaseEntityJpaSupport implements Serializable {

private static final long serialVersionUID = 1L;

    //@Id
    //@Column(name = "LOCALE_ID")
    @Basic(optional = false)
    @NotNull
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer localeId;

    @Size(max = 2)
    @Column(name = "LANGUAGE_CODE")    
    private String languageCode;

    public LocaleJpaImpl(Integer localeId) { this.localeId = localeId; }    
    public int getLocaleId() { return localeId; }    
    public void setLocaleId(Integer localeId) { this.localeId = localeId; }      
    public String getLanguageCode() { return languageCode; }    
    public void setLanguageCode(String languageCode) { this.languageCode = languageCode; }
}

3.接口(interface) LocaleJpaRepository

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.engine.i18n.domain.LocaleJpaImpl;

public interface LocaleJpaRepository extends JpaRepository<LocaleJpaImpl, Long>, LocaleJpaRepositoryCustom {

    @Query("FROM LocaleJpaImpl L WHERE L.languageCode = :languageCode")
    List<LocaleJpaImpl> findLocaleByLanguageCode(@Param("languageCode") String languageCode);   

}

4.接口(interface) LocaleJpaRepositoryCustom

import java.util.List;
import com.util.DAOSystemException;

public interface LocaleJpaRepositoryCustom {

    List<String> catchLanguagesCombinations() throws DAOSystemException;
}

5. LocaleJpaRepositoryImplemented

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.util.DAOSystemException;

public class LocaleJpaRepositoryImplemented implements LocaleJpaRepositoryCustom {

    @PersistenceContext(unitName = "contentEntityManagerFactory")
    private EntityManager em;

    @SuppressWarnings("unchecked")
    @Override
    public List<String> catchLanguagesCombinations() throws DAOSystemException {

        return "result";
    }
}

最佳答案

我遇到了这样的问题,我的错误是自定义存储库类的名称:

如果您的 jpa 存储库接口(interface)的名称是 LocaleJpaRepository,则您的新自定义接口(interface)应命名为 LocaleJpaRepositoryCustom,但在方法中进行覆盖的类必须命名LocaleJpaRepositoryImpl,如下:

public class LocalJpaRepositoryImpl implements LocalJpaRepositoryCustom{
@Override
   public void customMethod(){....}
}

基本上,您的自定义接口(interface)的实现类应该以您的存储库接口(interface) (JPARepository) 的名称开头,并以“Impl”关键字结尾。

关于spring - org.springframework.data.mapping.PropertyReferenceException : No property catch found for type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20777785/

有关spring - org.springframework.data.mapping.PropertyReferenceException : No property catch found for type的更多相关文章

  1. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  2. ruby-on-rails - 带 Spring 锁的 Rails 4 控制台 - 2

    我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.

  3. ruby-on-rails - 更好的替代方法 try( :output). try( :data). try( :name)? - 2

    “输出”是一个序列化的OpenStruct。定义标题try(:output).try(:data).try(:title)结束什么会更好?:) 最佳答案 或者只是这样:deftitleoutput.data.titlerescuenilend 关于ruby-on-rails-更好的替代方法try(:output).try(:data).try(:name)?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.c

  4. spring.profiles.active和spring.profiles.include的使用及区别说明 - 2

    转自:spring.profiles.active和spring.profiles.include的使用及区别说明下文笔者讲述spring.profiles.active和spring.profiles.include的区别简介说明,如下所示我们都知道,在日常开发中,开发|测试|生产环境都拥有不同的配置信息如:jdbc地址、ip、端口等此时为了避免每次都修改全部信息,我们则可以采用以上的属性处理此类异常spring.profiles.active属性例:配置文件,可使用以下方式定义application-${profile}.properties开发环境配置文件:application-dev

  5. ruby - 在 ruby​​ 中使用 .try 函数和 .map 函数 - 2

    我需要从json记录中获取一些值并像下面这样提取curr_json_doc['title']['genre'].map{|s|s['name']}.join(',')但对于某些记录,curr_json_doc['title']['genre']可以为空。所以我想对map和join()使用try函数。我试过如下curr_json_doc['title']['genre'].try(:map,{|s|s['name']}).try(:join,(','))但是没用。 最佳答案 你没有正确传递block。block被传递给参数括号外的方法

  6. ruby - 不能将 `each` 的所有或大多数情况替换为 `map` 吗? - 2

    Enumerable#each和Enumerable#map的区别在于返回的是接收者还是映射后的结果。回到接收者是微不足道的,你通常不需要在each之后继续一个方法链,比如each{...}.another_method(我可能没见过这样的案例。即使你想回到接收者那里,你也可以通过tap来实现)。所以我认为所有或者大部分使用Enumerable#each的情况都可以用Enumerable#map代替。我错了吗?如果我是对的,each的目的是什么?map是否比each慢?编辑:我知道当您对返回值不感兴趣时​​使用each是一种常见的做法。我对这种做法是否存在不感兴趣,但感兴趣的是,除了从

  7. ruby-on-rails - Spring 不起作用。 [未初始化常量 Spring::SID::DL] - 2

    我无法运行Spring。这是错误日志。myid-no-MacBook-Pro:myid$spring/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/lib/spring/sid.rb:17:in`fiddle_func':uninitializedconstantSpring::SID::DL(NameError)from/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/li

  8. ruby - `map` 比 `each` 快吗? - 2

    map遍历数组是否比each更快?两者有速度差异吗?mapresult=arr.map{|a|a+2}每个result=[]arr.eachdo|a|result.push(a+2)end 最佳答案 我认为是的。我试过这个测试require"benchmark"n=10000arr=Array.new(10000,1)Benchmark.bmdo|x|#Mapx.reportdon.timesdoresult=arr.map{|a|a+2}endend#Eachx.reportdon.timesdoresult=[]arr.each

  9. 【云原生】SpringCloud-Spring Boot Starter使用测试 - 2

    目录SpringBootStarter是什么?以前传统的做法使用SpringBootStarter之后starter的理念:starter的实现: 创建SpringBootStarter步骤在idea新建一个starter项目、直接执行下一步即可生成项目。 在xml中加入如下配置文件:创建proterties类来保存配置信息创建业务类:创建AutoConfiguration测试如下:SpringBootStarter是什么? SpringBootStarter是在SpringBoot组件中被提出来的一种概念、简化了很多烦琐的配置、通过引入各种SpringBootStarter包可以快速搭建出一

  10. ruby - 用于 Ruby 哈希的 map_values()? - 2

    我想念Ruby中的Hash方法来仅转换/映射散列值。h={1=>[9,2,3,4],2=>[6],3=>[5,7,1]}h.map_values{|v|v.size}#=>{1=>4,2=>1,3=>3}你如何在Ruby中归档它?更新:我正在寻找map_values()的实现。#moreexamplesh.map_values{|v|v.reduce(0,:+)}#=>{1=>18,2=>6,3=>13}h.map_values(&:min)#=>{1=>2,2=>6,3=>1} 最佳答案 Ruby2.4引入了方法Hash#tran

随机推荐