如何处理PropertyReferenceException

时间:2016-02-02 18:03:55

标签: spring spring-data-jpa

我已经提到各种来源,但我的问题仍未解决, 自定义存储库

public interface BaseRepositoryCustom<ContactDTO,Long> {
    List<ContactDTO> getTestData(String name);
}

默认地将Impl

public class BaseRepositoryImpl implements BaseRepositoryCustom {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<ContactDTO> getTestData(String name) {
        List<ContactDTO> contact = entityManager.createQuery("select * from COM_CONTACT").getResultList();
        System.out.println(contact.size());
        return null;
    }

主存储库

public interface ConContactRepository extends JpaRepository<Contact, Long>,BaseRepositoryCustom {
    List<ComContact> getTestData(String name);

}

我将得到DTO列表作为我的结果我的主要存储库是联系人类型,我该如何解决这个问题 例外

    ... 18 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property getTestData found for type ComContact!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353)
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:84)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:61)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:94)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:205)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:72)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:369)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:192)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 28 common frames omitted

联系DTO

public class ContactDTO {
    private String serviceName;
    private String contactName;
    private String title;
    }

联系域名模型

@Entity
@Table(name = "COM_CONTACT", schema = "JMS_SCHEMA")
public class ComContact implements java.io.Serializable {

    private long id;
    private String serviceNm;
    private String contactNm;
    private String title;
    private long fkTemplateId;
    private Character mailFlag;
    private String mailAddress;
    private Character faxFlag;
    private String faxNr;
}

2 个答案:

答案 0 :(得分:1)

首先,您不需要BaseRepositoryCustom。

你说你正在使用Spring Data JPA。

如果使用@Repository注释ConContactRepository。

类ComContact没有属性“name”,因此我们假设您要返回serviceNm等于“xxx”的所有ComContact对象。

在您的存储库中,创建一个名为FindAllByServiceNm(String serviceNm)的方法,它应该可以工作,不需要SQL。

如果您想获取所有记录,请尝试使用findAll,它应该可以正常工作。

获得所需的记录后,如果适用,您可以将它们转换为ContactDTO对象。

答案 1 :(得分:-1)

public interface ConContactRepository extends JpaRepository<Contact, Long>, ConContactRepositoryCustom {
  List<ContactDTO> getTestData(String name);
}

public interface ConContactRepositoryCustom {
  List<ContactDTO> getTestData(String name);
}

public class ConContactRepositoryImpl implements ConContactRepositoryCustom {

  @PersistenceContext
  private EntityManager entityManager;

  @Override
  public List<ContactDTO> getTestData(String name) {
    List<ContactDTO> contact = entityManager.createQuery("select * from COM_CONTACT").getResultList();
    System.out.println(contact.size());
    return null;
  }
}