spring org.springframework.data.mapping.PropertyReferenceException:找不到类型的属性捕获
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20777785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.springframework.data.mapping.PropertyReferenceException: No property catch found for type
提问by Andrea T
I have a issue with Spring Data repositories.
我有 Spring Data 存储库的问题。
When I deploy, I get an exception and it's because Spring Data tries to derive dynamically the method, but can't find in the Entity the corresponding property.
当我部署时,出现异常,这是因为 Spring Data 尝试动态派生该方法,但在实体中找不到相应的属性。
How can I put a custom method in the Custom Repository without this issue?
如何在没有此问题的情况下将自定义方法放入自定义存储库?
These are the involved components:
这些是涉及的组件:
LocaleJpaImpl
: the EntityLocaleJpaRepositoryClient
: the business layer classinterface LocaleJpaRepository extends JpaRepository<LocaleJpaImpl, Long>, LocaleJpaRepositoryCustom
interface LocaleJpaRepositoryCustom
LocaleJpaRepositoryImplemented implements LocaleJpaRepositoryCustom
LocaleJpaImpl
: 实体LocaleJpaRepositoryClient
: 业务层类interface LocaleJpaRepository extends JpaRepository<LocaleJpaImpl, Long>, LocaleJpaRepositoryCustom
interface LocaleJpaRepositoryCustom
LocaleJpaRepositoryImplemented implements LocaleJpaRepositoryCustom
LocaleJpaRepositoryCustom
has a method:
LocaleJpaRepositoryCustom
有一个方法:
List<String> catchLanguagesCombinations() throws DAOSystemException;
(LanguagesCombinations isn't a property of LocaleJpaImpl. For that motive is in the Custom Repository).
(LanguagesCombinations 不是 LocaleJpaImpl 的属性。因为这个动机在自定义存储库中)。
This 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
This is the relevant code:
这是相关代码:
1. LocaleJpaImpl:
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
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. interface LocaleJpaRepositoryCustom
4.接口LocaleJpaRepositoryCustom
import java.util.List;
import com.util.DAOSystemException;
public interface LocaleJpaRepositoryCustom {
List<String> catchLanguagesCombinations() throws DAOSystemException;
}
5. LocaleJpaRepositoryImplemented
5. LocaleJpaRepository已实现
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";
}
}
回答by Koitoer
I had a issue like this and my mistake was the name of the custom repository class:
我有一个这样的问题,我的错误是自定义存储库类的名称:
If the name of your jpa repository interface is LocaleJpaRepository
, your new custom interface should be named LocaleJpaRepositoryCustom
, but the class that makes the override in the method must be named LocaleJpaRepositoryImpl
, as it follows:
如果您的 jpa 存储库接口的名称为LocaleJpaRepository
,则您的新自定义接口应命名为LocaleJpaRepositoryCustom
,但在方法中进行覆盖的类必须命名LocaleJpaRepositoryImpl
为 ,如下所示:
public class LocalJpaRepositoryImpl implements LocalJpaRepositoryCustom{
@Override
public void customMethod(){....}
}
Basically, the implementation class of your custom interface should start with the name of your repository interface (JPARepository) ending with 'Impl' keyword.
基本上,您的自定义接口的实现类应该以您的存储库接口 (JPARepository) 的名称开头,并以 'Impl' 关键字结尾。
回答by user3531698
Custom Repository Implementation class name should be Interface name which is extending JPARepositry with Impl
自定义存储库实现类名称应该是接口名称,它使用 Impl 扩展 JPARepositry