Java 使用Spring数据合并要映射的两个对象
我有一个实体:Java 使用Spring数据合并要映射的两个对象,java,spring,hibernate,spring-data-jpa,spring-data,Java,Spring,Hibernate,Spring Data Jpa,Spring Data,我有一个实体: @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 =
@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数据存储库处理实体,我有一个方法,可以计算餐厅的投票数:
@Repository
public interface VoteRepository extends CrudRepository<Vote, Integer> {
Integer countByRestaurantId(Integer restaurantId);
}
@存储库
公共接口VoteRepository扩展了Crudepository{
整型countByRestaurantId(整型餐厅);
}
现在我想从我的控制器中获取投票统计信息,例如,我可以得到一个HashMap
,其中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);
}
}
@RestController
@RequestMapping(value=ADMIN\u voces\u URL,products=JSON\u TYPE)
公共类AdminVoteController实现控制器{
私有最终记录器log=LoggerFactory.getLogger(getClass());
@自动连线
私人VoteService VoteService;
@GetMapping(“/statistics”)
公共映射GetVoTestistics(@PathVariable Integer restaurantId){
log.info(“计算餐厅{}的票数”,restaurantId);
return voteService.getvotestistic(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);
@Query(“从投票v中选择不同的v.date,count(v.id),其中v.restaurant.id=?按v.date分组”)
映射findAllByDateAndRestaurantId(整数餐厅);
我如何使用Spring数据来实现它?你也可以为我提供另一种方法,谢谢 这里是最简单的修复方法 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);
@Query(“从投票v中选择新的ru.topjava.gradication.model.dto.VoteTo(v.date,count(v.id))”+
“其中v.restaurant.id=?按v.date划分的1组”)
列出groupCountByData(Integer restaurantId);
其中ru.topjava.gradication.model.dto.VoteTo
是我的dto类的完整路径(包+类的名称)