2016-02-02 81 views
0

我已经下文称各种消息来源,但我的问题仍然没有解决自定义方法, 自定义库找不到春天的数据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; 
} 
+1

'ContactDTO'不是一个实体,而是你的结果返回'entity'对象,所以你需要'Entity_To_bean'的结果 –

+2

变换,这是非常令人困惑。在'ConContactRepository'中,您使用'Contact'作为JpaRepository的实体类型,'BaseRepositoryCustom'中使用'ContactDTO',并且使用'ComContact'作为方法的返回类型。你应该决定,你想在你的版本库中使用哪种类型(并且它只应该是其中的一种)。 – dunni

+0

除了上面的评论,我鼓励你不要对非getter方法使用'get'预处理方法。只要以其他方式来命名。 –

回答

1

首先你不需要BaseRepositoryCustom的。

你说你在使用Spring Data JPA。

如果使用@Repository注释您的ConContactRepository。

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

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

如果你想获得所有记录,请尝试findAll,它应该工作。

一旦获得您想要的记录,您可以将它们转换为ContactDTO对象(如果适用)。

+0

你能否请求我建议我怎样才能定义一个自定义的方法,我想实现复杂的查询。 –

+0

在这种情况下,请在存储库中创建方法并使用@Query(“SELECT A from B where .....”)对其进行注释。您不必实现存储库,Spring将为您处理它。 –

+0

如果我不想在那种情况下使用@query,请 –

-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; 
    } 
}