【问题标题】:org.springframework.data.mapping.PropertyReferenceException: No property catch found for typeorg.springframework.data.mapping.PropertyReferenceException:没有找到类型的属性捕获
【发布时间】:2014-01-13 16:38:31
【问题描述】:

我对 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 的属性。因为该动机在自定义存储库中)。

这个例外:

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.接口 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.接口 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";
    }
}

【问题讨论】:

  • @Param("countryCode") 应该是@Param("languageCode")
  • @Abhinav Sarkar:谢谢!这是另一个错误。 :) 无论如何不是异常的原因。 :(

标签: spring spring-data


【解决方案1】:

自定义存储库实现类名称应该是接口名称,它使用 Impl 扩展 JPARepositry

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

    • 你是对的:实现 RepositoryCustom 接口的类有一个错误的名称 LocaleJpaRepositoryImplemented,它应该是 LocaleJpaRepositoryImpl 遵循 Spring-data 约定。修改类名解决了这个问题。非常感谢!
    • 我正在做同样的事情,但我有一个错误。它正在理解我创建的自定义方法作为实体存储库的一个字段。原因:org.springframework.data.mapping.PropertyReferenceException:找不到类型“ENTITY”的属性“METHODNAME”!你有什么想法吗?
    • 使用 @Query 注释重命名方法。它们不应该像普通的“findAll...”等
    • 谢谢@Koitoer 你救了
    • 需要注意的是,根据 Spring 文档,你的类名应该是 LocalJpaRepositoryCustomImpl 而不是 LocalJpaRepositoryImpl。但只有将其命名为 LocalJpaRepositoryImpl 才能使其工作,因此这意味着 Spring documentation 是错误的。引用:class CustomizedUserRepositoryImpl implements CustomizedUserRepository {
    猜你喜欢
    • 2016-10-28
    • 1970-01-01
    • 2020-07-14
    • 2019-04-08
    • 2017-05-17
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    相关资源
    最近更新 更多