Spring数据一对多select查询
Spring data one-to-many select query
我正在尝试为多对一关系配置 hql 并面临下一个问题:spring 如果今天的票数小于 1,则数据不会 select 餐厅。在数据库中一切正常
结果应该是
{
"id": 3,
"name": "Kroger",
"votes": 2
},
{
"id": 2,
"name": "Fridays",
"votes": 1
},
{
"id": 4,
"name": "Wingstop",
"votes": 1
},
{
"id": 1,
"name": "Macdonalds",
"votes": 0
}
但它是
{
"id": 3,
"name": "Kroger",
"votes": 2
},
{
"id": 2,
"name": "Fridays",
"votes": 1
},
{
"id": 4,
"name": "Wingstop",
"votes": 1
}
餐厅实体
@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;
}
}
投票实体
@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;
}
}
餐厅资料库
@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();
}
如果您需要更多信息,请告诉我。
问题已解决。
解决方案:
@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;
}
}
和repo的方法
@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();
我正在尝试为多对一关系配置 hql 并面临下一个问题:spring 如果今天的票数小于 1,则数据不会 select 餐厅。在数据库中一切正常
结果应该是
{
"id": 3,
"name": "Kroger",
"votes": 2
},
{
"id": 2,
"name": "Fridays",
"votes": 1
},
{
"id": 4,
"name": "Wingstop",
"votes": 1
},
{
"id": 1,
"name": "Macdonalds",
"votes": 0
}
但它是
{
"id": 3,
"name": "Kroger",
"votes": 2
},
{
"id": 2,
"name": "Fridays",
"votes": 1
},
{
"id": 4,
"name": "Wingstop",
"votes": 1
}
餐厅实体
@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;
}
}
投票实体
@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;
}
}
餐厅资料库
@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();
}
如果您需要更多信息,请告诉我。
问题已解决。 解决方案:
@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;
}
}
和repo的方法
@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();