gpt4 book ai didi

java - 谁可以进行这个 JPQL 查询?

转载 作者:行者123 更新时间:2023-12-02 03:26:16 25 4
gpt4 key购买 nike

我的 JPA 实体类如下所示:

@Entity
@Table(name = "users")
public class User implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private Long id;

...

// bi-directional many-to-many association to Tag
@ManyToMany
@JoinTable(name = "user_tags_preferences", joinColumns = {
@JoinColumn(name = "user_id") },
inverseJoinColumns = {
@JoinColumn(name = "tag_id") })
private List<Tag> tags;
@Entity
@Table(name = "tags")
public class Tag implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private Long id;

private String name;

...

// bi-directional many-to-many association to CookEvent
@ManyToMany(mappedBy = "tags")
private List<CookEvent> cookEvents;

// bi-directional many-to-many association to User
@ManyToMany(mappedBy = "tags")
private List<User> users;
@Entity
@Table(name = "cook_events")
public class CookEvent implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private Long id;

@Column(name = "takes_place_on")
private LocalDateTime takesPlaceOn;

...

// bi-directional many-to-many association to Tag
@ManyToMany
@JoinTable(name = "cook_events_tags", joinColumns = {
@JoinColumn(name = "cook_event_id") },
inverseJoinColumns = {
@JoinColumn(name = "tag_id") })
@OrderBy("name")
private List<Tag> tags;

在我的数据库中,我有“users”、“cook_events”、“tags”、“user_tags_preferences”、“cook_events_tags”表。我需要执行以下操作的 JPQL 查询:

从我的前端我有 user_id。我想要一个过滤所有takesPlaceOn > CURRENT_TIMESTAMP的cook_events的查询,并且我的 user_id 中至少有一个 user_tags_preferences 与 Cook_event_tags 相匹配。

是否有可能通过连接来过滤所有这些,最终得到 List<CookEvent>我需要。甚至是List<Tag>如果更容易的话。

它甚至可以是不同的查询,我自己在后端过滤它,但我想尝试过滤查询中的所有内容。

最佳答案

这就是答案吗?

SELECT DISTINCT(c) from User u JOIN u.tags t JOIN t.cookEvents c
WHERE u.id = :id AND c.takesPlaceOn > CURRENT_TIMESTAMP

关于java - 谁可以进行这个 JPQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56906333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com