小编典典

Spring Data JPA自定义方法导致PropertyReferenceException

spring-boot

我一直在尝试创建一个Spring Boot应用程序。在我的应用程序中,我想添加一些自定义方法来保存数据,而不是使用默认的save方法。

我的应用程序入口点是这样的:

@Configuration
@ComponentScan
@EnableJpaRepositories(repositoryImplementationPostfix = "CustomImpl")
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
@PropertySource("application.properties")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);         
    }

}

我已经将此行repositoryImplementationPostfix更改为Impl,但是没有用。

我的Crud仓库

@RepositoryRestResource
public interface TaRepository extends CrudRepository<Ta, Integer> ,TestRepository{


    List<Ta> findByName(@Param("name") String name);

}

我的自定义存储库:

public interface TestRepository {
    public void myCustomMethod(TestDto dto);
}

我的自定义存储库Impl

public class TestRepositoryCustomImpl implements TestRepository{

    @PersistenceContext
    private EntityManager em;
    @Override
    public void myCustomMethod(TestDto model){
}

注意:

如果我将CrudRepostory从上述内容更改为以下内容:

 @RepositoryRestResource
    public interface TaRepository extends CrudRepository<Ta, Integer> {


        List<Ta> findByName(@Param("name") String name);

    }

一切正常。但是不能使用自定义方法实现。


阅读 580

收藏
2020-05-30

共1个答案

小编典典

对于Spring Data
JPA,@Repository或者@RepositoryRestResource您不需要实现自定义接口。对于任何简单的查询,您都可以创建任何一种方法,请遵循简单的指南。

http://docs.spring.io/spring-
data/jpa/docs/1.4.1.RELEASE/reference/html/jpa.repositories.html

对于复杂的查询,可以使用JpaSpecificationExecutor。

如何从HQL查询创建谓词?

2020-05-30