gpt4 book ai didi

java - 需要帮助创建条件子查询

转载 作者:行者123 更新时间:2023-11-30 22:12:04 26 4
gpt4 key购买 nike

我是 jpa 的初学者,我遇到过复合查询。然后我开始搜索解决方案。许多人建议使用标准 API。

所以问题是:如何使用 criteriaQuery 创建类似的查询?

MySQL版本

SELECT role FROM Role WHERE id_role=(SELECT id_role FROM Client WHERE email=:email)

作用

@Entity
@Table(name="role")
public class Role {

@Id
public int id_role;

private enum enumRole {ADMIN, CLIENT}

@Column(name="role")
private enumRole role;

public Role() {
}

public Role(enumRole role) {
this.role = role;
}

public String getRole() {
return this.role.name();
}

public void setRole(String role) {
this.role = enumRole.valueOf(role);
}
@OneToMany(targetEntity = Client.class)
private List clientList;
}

客户端

@Entity
@Table(name ="client")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long Id;

@Column(name="first_name", length = 25)
private String firstName;

@Column(name="surname", length = 25)
private String surName;

@Column(name="password", length = 30)
private String password;
@Column(name="email")
private String email;

@Column(name="username", length = 15)
private String username;

public Client() {
}

@ManyToMany(targetEntity = Ticket.class)
private Set ticketSet;

public Client(String firstName, String surName, String password, String email, String username) {
this.firstName = firstName;
this.surName = surName;
this.password = password;
this.email = email;
this.username = username;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getSurName() {
return surName;
}

public void setSurName(String surName) {
this.surName = surName;
}


public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

}
}

最佳答案

能不能在Client和Role之间建立一个映射? @OneToMany 还是@ManyToMany?如果是这样,那么您不需要编写任何 sql 并且可以使用简单的 JPA 查询方法来完成 http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation

关于java - 需要帮助创建条件子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39769888/

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