[英]How to execute a stored procedure with JPA's crudrepository with Spring boot?[PropertyReferenceException]
I try to execute a postgres stored procedure which looks like that: 我尝试执行如下所示的postgres存储过程:
CREATE OR REPLACE FUNCTION get_subjects()
RETURNS SETOF subject AS
$$
SELECT * FROM subject;
$$
LANGUAGE 'sql';
I have a POJO class for subjects, a repository, and a service defined as follows: 我有一个用于主题,存储库和定义如下的服务的POJO类:
@Entity
@Table(name = "subject")
@NamedStoredProcedureQueries({
@NamedStoredProcedureQuery(name = "get_subjects",
procedureName = "get_subjects")
})
public class Subject {
@Id
@Column(name = "uid")
@Type(type = "pg-uuid")
private UUID uid;
private String name;
private String start_date;
private String end_date;
protected Subject(){
}
public Subject(UUID uid, String name, String start_date, String end_date) {
this.uid = uid;
this.name = name;
this.start_date = start_date;
this.end_date = end_date;
}
//getter setter
@Repository
public interface SubjectRepository extends CrudRepository<Subject, Long> {
@Procedure(name = "get_subjects")
Collection<Subject> retrieveSubjects();
}
@Service
public class SubjectService {
private final SubjectRepository subjectRepository;
@Autowired
public SubjectService(SubjectRepository subjectRepository) {
this.subjectRepository = subjectRepository;
}
public Collection<Subject> getAllSubjects(){
return subjectRepository.retrieveSubjects();
}
}
The error caused by: 由以下原因引起的错误:
org.springframework.data.mapping.PropertyReferenceException: No property retrieveSubjects found for type Subject!
org.springframework.data.mapping.PropertyReferenceException:未找到类型为Subject的属性retrieveSubjects!
I tried to implement a SubjectRepositoryCustom, where I got a nullpointer back on Collection<Subject>
. 我试图实现SubjectRepositoryCustom,在
Collection<Subject>
上返回了一个空指针。 I went through some of the tutorials such as https://dzone.com/articles/calling-stored-procedures-from-spring-data-jpa . 我浏览了一些教程,例如https://dzone.com/articles/calling-stored-procedures-from-spring-data-jpa 。 I also have a stored procedure running on one of my other repository where I insert with a stored procedure, which works excellent.
我还有一个存储过程正在其他存储库中的一个上运行,我在其中插入了一个存储过程,效果很好。
Could someone point out what cause the error and give a sample solution? 有人可以指出导致错误的原因并提供示例解决方案吗?
For your retrieveSubjects method you can use findAll() from CrudRepository. 对于您的retrieveSubjects方法,可以使用CrudRepository中的findAll()。 The get_subjects() procedure is not required in this simple case.
在这种简单情况下,不需要get_subjects()过程。 See first example from https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html
请参阅https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html中的第一个示例
For procedures I am using nativeQuery and let Spring to map the result correctly. 对于过程,我使用nativeQuery并让Spring正确映射结果。
@Repository
public interface MyRepository extends CrudRepository<MyModel, Long> {
@Query(value = "CALL my_procedure()", nativeQuery = true)
List<MyModel> myProcedure();
}
If your procedure retrieves some computed/custom rows make sure that it retrieves an unique value for the @Id variable in each row. 如果您的过程检索了一些计算的/自定义的行,请确保该过程为每行中的@Id变量检索一个唯一值。 I had mapping issues using a wrong @Id column for a custom procedure result and ended using a temporary table with an unused primary key auto increment column.
我在使用错误的@Id列获得自定义过程结果时遇到了映射问题,并使用带有未使用的主键自动增量列的临时表结束了映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.