【问题标题】:Merging two Objects to Map using Spring Data使用 Spring Data 将两个对象合并到 Map
【发布时间】:2020-01-28 17:12:59
【问题描述】:

我有一个实体:

@Entity
@Table(name = "votes")
public class Vote extends AbstractBaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private LocalDate date = LocalDate.now();

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "restaurant_id", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    private Restaurant restaurant;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    public Vote() {
    }

    public Vote(User user, Restaurant restaurant) {
        this.user = user;
        this.restaurant = restaurant;
    }

    //getters and setters
}

我使用 Spring Data Repository 来处理实体,并且我有一个方法可以计算餐厅的票数:

@Repository
public interface VoteRepository extends CrudRepository<Vote, Integer> {

    Integer countByRestaurantId(Integer restaurantId);
}

现在我想从我的控制器获取投票的统计信息例如 - 我可以得到一个 HashMap &lt;LocalDate, Integer&gt;,其中 Integer 是一个特殊日期的投票数:

@RestController
@RequestMapping(value = ADMIN_VOTES_URL, produces = JSON_TYPE)
public class AdminVoteController implements Controller {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private VoteService voteService;

    @GetMapping("/statistics")
    public Map<LocalDate, Integer> getVoteStatistics(@PathVariable Integer restaurantId) {
        log.info("get count of the votes for restaurant {}", restaurantId);
        return voteService.getVoteStatistic(restaurantId);
    }
}

这是我对存储库的不成功请求:

@Query("select distinct v.date, count(v.id) from Vote v where v.restaurant.id = ?1 group by v.date")
    Map<LocalDate, Integer> findAllByDateAndRestaurantId(Integer restaurantId);

如何使用 Spring Data 做到这一点?你也可以给我另一种方法,谢谢!

【问题讨论】:

    标签: java spring hibernate spring-data-jpa spring-data


    【解决方案1】:

    这是最简单的修复方法!

    1) 创建 DTO:

    public class VoteTo implements Serializable {
    
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private LocalDate date;
    
        private Long count;
    
        public VoteTo() {
        }
    
        public LocalDate getDate() {
            return date;
        }
    
        public void setDate(LocalDate date) {
            this.date = date;
        }
    
        public Long getCount() {
            return count;
        }
    
        public void setCount(Long count) {
            this.count = count;
        }
    
        public VoteTo(LocalDate date, Long count) {
            this.date = date;
            this.count = count;
        }
    }
    

    2) 在我创建此方法的同一个存储库中

    @Query("select new ru.topjava.graduation.model.dto.VoteTo(v.date, count(v.id)) from Vote v " +
    " where v.restaurant.id = ?1 group by v.date")
    List<VoteTo> groupCountByData(Integer restaurantId);
    

    ru.topjava.graduation.model.dto.VoteTo 是我的 DTO 类的完整方式(包 + 类的名称)

    【讨论】:

      猜你喜欢
      • 2020-01-16
      • 2019-08-15
      • 2022-07-08
      • 2021-08-17
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      相关资源
      最近更新 更多