【发布时间】:2018-03-14 09:43:03
【问题描述】:
我尝试执行如下所示的 postgres 存储过程:
CREATE OR REPLACE FUNCTION get_subjects()
RETURNS SETOF subject AS
$$
SELECT * FROM subject;
$$
LANGUAGE 'sql';
我有一个用于主题、存储库和服务的 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();
}
}
导致的错误:
org.springframework.data.mapping.PropertyReferenceException: 否 找到类型主题的属性retrieveSubjects!
我尝试实现一个 SubjectRepositoryCustom,我在 Collection<Subject> 上得到了一个空指针。我浏览了一些教程,例如https://dzone.com/articles/calling-stored-procedures-from-spring-data-jpa。
我还有一个存储过程在我的另一个存储库上运行,我在其中插入了一个存储过程,效果很好。
有人能指出导致错误的原因并给出示例解决方案吗?
【问题讨论】:
-
一切看起来都不错,您使用的是什么版本的 spring-data-jpa? JPA 中的调用过程也在 2.1 中引入。顺便说一句,您的 id 是
UUID类型,但在您的存储库中它是Long
标签: java jpa spring-boot stored-procedures