【问题标题】:Spring Data Rest Hateos Return Extra ColumnSpring Data Rest Hateoas 返回额外的列
【发布时间】:2017-08-10 17:47:29
【问题描述】:

使用 SDR。我有一个基于连接表的实体 PersonRole。对于报告,我想要连接表连接的表的两个不同列之间的最小日期。我在内网工作,所以她是一些伪代码...

select 
pr, 
if pr.dateOne < pr.dateTwo then pr.dateOne else pr.dateTwo end as minDate
from 
PersonRole pr
...

现在,当我只关心获取 DTO 数据而不关心与 HATEOAS 相关的数据时,我已经使用 DTO 来获取额外的数据以及我的实体。

基本上我想要这样的东西......

personRole : [ {
 person : {
   dateOne: blah
 },
 role : {
   dateTwo: blah
 },
 minDate: this is a min of person.dateOne and role.dateTwo
}]

【问题讨论】:

    标签: spring-data spring-data-rest spring-hateoas


    【解决方案1】:

    您可以为您的实体添加一个新方法,该方法将计算 minDate,例如:

    @JsonProperty
    public Date getMinDate() {
       if (dateOne < dateTwo)
           return dateOne;
       else
           return dateTwo;
    }
    

    Spring Data REST 将采用此方法并将其与您的实体一起序列化。

    【讨论】:

      【解决方案2】:

      我可能有点晚了,但是这种功能可以通过投影和@Value 注解来实现。如果您将投影定义如下,它将起作用:

      @Projection(name="report",types={PersonRole.class})
      public interface PersonRoleProjection{
      
         @Value("target.dateOne < target.dateTwo ? target.dateOne : target.dateTwo")
         Date getMinDate()
      
         .... Define other getters here...
      }
      

      【讨论】:

        猜你喜欢
        • 2013-10-31
        • 2014-03-07
        • 2014-08-16
        • 2014-07-05
        • 2016-05-23
        • 2018-07-23
        • 1970-01-01
        • 2018-11-26
        • 1970-01-01
        相关资源
        最近更新 更多