映射使用Hibernate返回表的postgres函数

时间:2018-11-26 13:09:10

标签: sql spring postgresql hibernate jpa

我已经搜索了整个stackoverflow和其他站点,但无法弄清楚如何在Spring应用程序中使用Hibernate映射从postgres函数返回的表。

我什至不确定postgres函数的返回类型表是否可以通过某种方式与MyCustomTable匹配。

我想做的是使用Hibernate从spring应用程序调用postgres函数(存储过程)。

我有这个postgres功能

@GetMapping("/users/{username}/func99")
    public List<MyCustomTable> getResultsFromFunc99(@PathVariable(value = "username") String username,
                                                 @CurrentUser UserPrincipal currentUser,
                                                 @RequestParam(value = "service_type") String type,
                                                 @RequestParam(value = "start_date") Timestamp startDate,
                                                 @RequestParam(value = "end_date") Timestamp endDate){

        return queryService.getResultsFromFunc99(username, currentUser, type, startDate, endDate);
    }

控制器

public List<MyCustomTable> getResultsFromFunc99(String username, UserPrincipal currentUser, String type, Timestamp startDate, Timestamp endDate) {

        User user = userRepository.findByUsername(username)
                .orElseThrow(() -> new ResourceNotFoundException("User", "username", username));


        return return incidentRepository.func99(type, startDate, endDate);

服务

@Procedure(procedureName = "func99")
    List<MyCustomTable> func99(String type, Timestamp startDate, Timestamp endDate);

存储库

@Entity
@NamedStoredProcedureQuery(
        name = "func99",
        procedureName = "func99",
        parameters = {
                @StoredProcedureParameter(name = "type", mode = ParameterMode.IN, type = String.class),
                @StoredProcedureParameter(name = "start_date", mode = ParameterMode.IN, type = Timestamp.class),
                @StoredProcedureParameter(name = "end_date", mode = ParameterMode.IN, type = Timestamp.class)
        }
)
@Table(name = "table1")
public class MyCustomTable {...}

实体

this.strategies = {
    1: this.first,
    2: this.second,
    3: this.third
}

当postgres函数返回整数时,我可以使其工作。我该如何映射来自postgres函数的表返回以及如何将其与Hibernate集成?

非常感谢您的帮助! 非常感谢!

2 个答案:

答案 0 :(得分:0)

这是我用于类似问题的解决方法,但它可能有效。

像这样定义SqlResultSetMapping:

@SqlResultSetMapping(
    name = "MyCustomTableMapping",
    entities = @EntityResult(entityClass = MyCustomTable.class)
)

然后将此参数更改为您的NamedStoredProcedureQuery批注:

resultSetMappings = "MyCustomTableMapping"

我将此技术与NamedNativeQueries配合使用,以确保Hibernate不会跟踪它们的变化,例如实际实体。使我和我的同事不必记住detach个实体。几乎每本有关如何使用与表格都不对应的搜索结果的教程都将使您面临这个问题,并将其视为可以。

答案 1 :(得分:0)

我找到了可行的解决方案。

我不使用存储库中的方法。 代替上述存储库:

*@Procedure(procedureName = "func99")
    List<MyCustomTable> func99(String type, Timestamp startDate, Timestamp endDate);*

我知道可以在不使用存储库的情况下在内部执行此操作:

@PersistenceContext
    EntityManager em;

public List<MyCustomTable> getResultsFromFunc99(String username, UserPrincipal currentUser, String type, Timestamp startDate, Timestamp endDate) {
    User user = userRepository.findByUsername(username)
            .orElseThrow(() -> new ResourceNotFoundException("User", "username", username));

 Query query = em.createNamedStoredProcedureQuery("func99");
        query.setParameter("some_day", someDay);
        ((StoredProcedureQuery) query).execute();

return query.getResultList()

实体类在我声明的地方保持不变

@Entity
@NamedStoredProcedureQuery(
        name = "func99",
        procedureName = "func99",
        parameters = {
                @StoredProcedureParameter(name = "type", mode = ParameterMode.IN, type = String.class),
                @StoredProcedureParameter(name = "start_date", mode = ParameterMode.IN, type = Timestamp.class),
                @StoredProcedureParameter(name = "end_date", mode = ParameterMode.IN, type = Timestamp.class)
        }
)
@Table(name = "table1")
public class MyCustomTable {...}