如何计算 Spring Boot 中的嵌套查询
How to count a nested query in Springboot
我正在尝试使用 springboot.Thus 计算每天登录失败的次数,目前我发现有 countBy 可用于计数,但是我不确定如何使用 countBy 执行此查询。
这就是我想要做的:
count all login attempts where date = today'sDate ,
loginID = userLoginID
and booleanLoginSuccess = false
这是我在我的 LoginRepository 中尝试的:
Long countByTodayDateAndLoginCredentialsIDAndloginSuccessStatusTrue( String currentDate, int loginId);
这是我得到的错误:
No property loginCredentialsIDAndloginSuccessStatus found for type LoginInformation!
这是我的登录信息实体:
@Entity
@Table(name = "LoginInformation")
public class LoginInformation {
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int loginID;
@JsonIgnore
private Boolean loginSuccessStatus;
private String deviceID;
private String appVersion;
private String deviceOS;
@JsonIgnore
private String todayDate;
@JsonIgnore
@ManyToOne
@JoinColumn(name="loginCredentialsID")
private UserLogin userLogin;
public LoginInformation() {
}
public LoginInformation(Boolean loginSuccessStatus, String deviceID, String appVersion, String deviceOS) {
this.loginSuccessStatus = loginSuccessStatus;
this.deviceID = deviceID;
this.appVersion = appVersion;
this.deviceOS = deviceOS;
}
public LoginInformation(Boolean loginSuccessStatus, String deviceID, String appVersion, String deviceOS, String todayDate, UserLogin userLogin) {
this.loginSuccessStatus = loginSuccessStatus;
this.deviceID = deviceID;
this.appVersion = appVersion;
this.deviceOS = deviceOS;
this.todayDate = todayDate;
this.userLogin = userLogin;
}
... omitted getter and setters for brevity
这是我的用户登录实体:
Entity
@Table(name = "UserLogin",
uniqueConstraints =
{
@UniqueConstraint(columnNames = "userName")
})
public class UserLogin implements Serializable, UserDetails {
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int loginCredentialsID;
private String username;
private String password;
@OneToMany(mappedBy = "userLogin", cascade = CascadeType.ALL)
private List<LoginInformation> loginInfo = new ArrayList();
public UserLogin(String username, String password) {
this.username = username;
this.password = password;
}
public UserLogin() {
}
... omitted getter and setters for brevity
应使用实体字段,而不是列名。
Long countByTodayDateAndUserLoginLoginCredentialsIDAndloginSuccessStatusTrue( String currentDate, int loginId);
userLogin 是对象,因此,要访问其 id,请在方法名称中添加 Id
我正在尝试使用 springboot.Thus 计算每天登录失败的次数,目前我发现有 countBy 可用于计数,但是我不确定如何使用 countBy 执行此查询。
这就是我想要做的:
count all login attempts where date = today'sDate ,
loginID = userLoginID
and booleanLoginSuccess = false
这是我在我的 LoginRepository 中尝试的:
Long countByTodayDateAndLoginCredentialsIDAndloginSuccessStatusTrue( String currentDate, int loginId);
这是我得到的错误:
No property loginCredentialsIDAndloginSuccessStatus found for type LoginInformation!
这是我的登录信息实体:
@Entity
@Table(name = "LoginInformation")
public class LoginInformation {
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int loginID;
@JsonIgnore
private Boolean loginSuccessStatus;
private String deviceID;
private String appVersion;
private String deviceOS;
@JsonIgnore
private String todayDate;
@JsonIgnore
@ManyToOne
@JoinColumn(name="loginCredentialsID")
private UserLogin userLogin;
public LoginInformation() {
}
public LoginInformation(Boolean loginSuccessStatus, String deviceID, String appVersion, String deviceOS) {
this.loginSuccessStatus = loginSuccessStatus;
this.deviceID = deviceID;
this.appVersion = appVersion;
this.deviceOS = deviceOS;
}
public LoginInformation(Boolean loginSuccessStatus, String deviceID, String appVersion, String deviceOS, String todayDate, UserLogin userLogin) {
this.loginSuccessStatus = loginSuccessStatus;
this.deviceID = deviceID;
this.appVersion = appVersion;
this.deviceOS = deviceOS;
this.todayDate = todayDate;
this.userLogin = userLogin;
}
... omitted getter and setters for brevity
这是我的用户登录实体:
Entity
@Table(name = "UserLogin",
uniqueConstraints =
{
@UniqueConstraint(columnNames = "userName")
})
public class UserLogin implements Serializable, UserDetails {
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int loginCredentialsID;
private String username;
private String password;
@OneToMany(mappedBy = "userLogin", cascade = CascadeType.ALL)
private List<LoginInformation> loginInfo = new ArrayList();
public UserLogin(String username, String password) {
this.username = username;
this.password = password;
}
public UserLogin() {
}
... omitted getter and setters for brevity
应使用实体字段,而不是列名。
Long countByTodayDateAndUserLoginLoginCredentialsIDAndloginSuccessStatusTrue( String currentDate, int loginId);
userLogin 是对象,因此,要访问其 id,请在方法名称中添加 Id