gpt4 book ai didi

java - 在 HQL 上加入声明

转载 作者:行者123 更新时间:2023-11-29 02:57:17 25 4
gpt4 key购买 nike

我有一个名为 Category 和 CategorySet 的表。

CREATE TABLE CATEGORY
(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
category_name VARCHAR(40) NOT NULL,
description VARCHAR(255),
uuid VARCHAR(40)
);

CREATE TABLE CATEGORY_SET
(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
parent INT NOT NULL,
child INT NOT NULL UNIQUE,
FOREIGN KEY (parent) REFERENCES CATEGORY (id),
FOREIGN KEY (child) REFERENCES CATEGORY (id)
);

分类.java

@Entity
@Table(name = "CATEGORY")
public class Category implements Serializable{
@Id
@Column(name = "ID")
@GeneratedValue
private Integer id;

@Column(name = "CATEGORY_NAME", nullable = false, length = 40)
private String name;

@Column(name = "DESCRIPTION", nullable = false, length = 255)
private String description;

@Column(name = "UUID", nullable = false, length = 40)
private String uuid;

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "CATEGORY_SET", joinColumns = {@JoinColumn(name = "PARENT")}, inverseJoinColumns = {@JoinColumn(name = "CHILD")})
private Collection<Category> subCategories = new LinkedHashSet<Category>();

public Category(String name, String description, String uuid, Collection<Category> categorySets) {
this.name = name;
this.description = description;
this.uuid = uuid;
this.categorySets = categorySets;
}
}

我想获取所有根类别,这意味着它们不是任何子类别。

得到结果的sql是:

SELECT DISTINCT CATEGORY.id
FROM CATEGORY
JOIN CATEGORY_SET
ON CATEGORY.id = CATEGORY_SET.parent
WHERE CATEGORY.id NOT IN (SELECT child
FROM CATEGORY_SET);

谁能帮我为此编写 HQL。

最佳答案

HQL 查询是:

select c
from Category c
where c.id not in (select child from CategorySet)

关于java - 在 HQL 上加入声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29328058/

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