简体   繁体   English

Spring 数据一对多 select 查询

[英]Spring data one-to-many select query

I'm trying to configure out hql for many-to-one relationship and facing next issue: spring data doesn't select restaurant if votes count for today is less than 1. In database everything's ok我正在尝试为多对一关系配置 hql 并面临下一个问题: spring 数据不 select 餐厅如果今天的票数小于 1。在数据库中一切正常

Result should be结果应该是

  {
    "id": 3,
    "name": "Kroger",
    "votes": 2
  },
  {
    "id": 2,
    "name": "Fridays",
    "votes": 1
  },
  {
    "id": 4,
    "name": "Wingstop",
    "votes": 1
  },
  {
    "id": 1,
    "name": "Macdonalds",
    "votes": 0
  }

But it is但它是

  {
    "id": 3,
    "name": "Kroger",
    "votes": 2
  },
  {
    "id": 2,
    "name": "Fridays",
    "votes": 1
  },
  {
    "id": 4,
    "name": "Wingstop",
    "votes": 1
  }

Restaurant entity餐厅实体

@Entity
@NoArgsConstructor
@Getter
@Setter
@Table(name = "restaurant", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Restaurant extends NamedEntity {
    @OneToMany(mappedBy = "restaurant", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JsonManagedReference
    private Set<Vote> votes;

    public Restaurant(Integer id, String name) {
        this.id = id;
        this.name = name;
    }
}

Vote entity投票实体

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "votes", uniqueConstraints = {@UniqueConstraint(columnNames = {"user_id", "date"}, name = "unique_user_id_per_date_idx")})
public class Vote extends BaseEntity {
    @Column(name = "date")
    @CreationTimestamp
    private LocalDate date;

    @ManyToOne
    @JoinColumn(name = "user_id")
    @OnDelete(action = OnDeleteAction.CASCADE)
    private User user;

    @ManyToOne
    @JoinColumn(name = "restaurant_id")
    @OnDelete(action = OnDeleteAction.CASCADE)
    private Restaurant restaurant;

    public Vote(Integer id, LocalDate date, User user, Restaurant restaurant) {
        super(id);
        this.date = date;
        this.user = user;
        this.restaurant = restaurant;
    }
}

Restaurant repository餐厅资料库

@Transactional(readOnly = true)
public interface RestaurantRepository extends BaseRepository<Restaurant> {

//    @Query("select r from Restaurant r join r.votes v where v.date=current_date")
//    @Query("from Restaurant r left join  r.votes v where v.date=current_date")
//    @Query("select r from Restaurant r join fetch Vote v on v.date=current_date")
//    Set<Restaurant> findAllForCurrentDate();

//    @Query("from Restaurant r left join fetch r.votes as v on v.date=current_date")

    @Query("from Restaurant r left outer join fetch r.votes as v where v.date=current_date")
    Set<Restaurant> findAllForCurrentDate();
}

If you need additional info please let me know.如果您需要其他信息,请告诉我。

Problem is solved.问题解决了。 Solution:解决方案:

@Entity
@NoArgsConstructor
@Getter
@Setter
@AllArgsConstructor
@Table(name = "restaurant", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
@NamedEntityGraph(name = "graph.Parent.children", attributeNodes = @NamedAttributeNode("votes")) // added this
public class Restaurant extends NamedEntity implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    @OneToMany(mappedBy = "restaurant",
            fetch = FetchType.EAGER,
            cascade = CascadeType.ALL)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JsonManagedReference
    private Set<Vote> votes = new HashSet<>();

    public Restaurant(Integer id, String name) {
        this.id = id;
        this.name = name;
    }
}

And repo's method和回购的方法

@Query("select distinct r from Restaurant r left join r.votes v on v.date=current_date")
    @EntityGraph(value = "graph.Parent.children") // added this
    Set<Restaurant> findAllForCurrentDate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM