2017-05-25 67 views
0

我試圖將自定義行爲添加到Spring數據存儲庫,但它不起作用。Spring數據:將自定義行爲添加到單個存儲庫

道:

public interface Dao extends JpaRepository<User, Long>, DaoCustom { 

} 

DaoCustom:

public interface DaoCustom { 

    User create(User user); 
} 

DaoCustomImpl:

@Repository 
@Transactional 
public class DaoCustomImpl implements DaoCustom { 

    @PersistenceContext 
    private EntityManager em; 

    @Override 
    public User create(User user) { 
     em.persist(user); 
     return user; 
    } 
} 

MainClass:

public class SpringDataJPAMain { 
    public static void main(String[] args) { 
     ApplicationContext context = new AnnotationConfigApplicationContext(RootConfig.class); 
     Dao dao = context.getBean(Dao.class); 

    } 
} 

當我運行MainClass時,拋出以下異常:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dao': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property create found for type User! 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:742) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) 
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84) 
    at testSpringDataJPA.config.SpringDataJPAMain.main(SpringDataJPAMain.java:10) 
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property create found for type User! 
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) 
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) 
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) 
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) 
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:247) 
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398) 
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:378) 
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:86) 
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:64) 
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103) 
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:214) 
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221) 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) 
    ... 11 more 

怎麼了?根據Spring的tutorial它應該可以正常工作。

回答

1

要在Spring Data Repository中實現您的定製存儲庫,您必須遵循Spring命名約定。所以,試試這個:

@Repository 
@Transactional 
public class DaoImpl implements DaoCustom { 

    @PersistenceContext 
    private EntityManager em; 

    @Override 
    public User create(User user) { 
     em.persist(user); 
     return user; 
    } 
} 

您的自定義庫中實現類的名稱應該是 'MainRepositoryClassName' + '默認地將Impl'

相關問題