我有两张table:User
和Wallet
User
具有:id
Wallet
具有:userId
、walletName
现在,我想避免这样的用户不能有两个同名钱包。
我创建了一些东西,但它适用于所有用户,所以如果用户与id 1
创建一个名为walletSaving
的钱包没有人可以创建该名称的钱包,虽然我想避免,我想创建一些东西来检查用户与id 1
是否已经有该名称的钱包。
到目前为止我有这个:
if (walletRepository.existsByWalletName(walletRequest.getWalletName())) {
return ResponseEntity.badRequest().body(new MessageResponse("You already have wallet with that name, choose another!"));
}
在一些帮助后,我尝试了这样的东西:
@PostMapping("/user/{user_id}/wallets")
public ResponseEntity<?> createWallet(@PathVariable(value = "user_id") Long user_id,
@RequestBody Wallet walletRequest, User user) {
if (walletRepository.existsByUserIdAndWalletName(user.getId(), walletRequest.getWalletName())) {
return ResponseEntity.badRequest()
.body(new MessageResponse("You already have wallet with that name, choose another!"));
}
它仍在创建同名钱包。
只是为了提供更多信息。
x1米10英寸1x
@Entity
@Table(name = "users",
uniqueConstraints = {
@UniqueConstraint(columnNames = "username"),
@UniqueConstraint(columnNames = "email")
})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotEmpty
@Size(min = 3, max = 20)
private String username;
@NotEmpty
@Size(max = 50)
@Email
private String email;
@NotEmpty
@Size(min = 6)
private String password;
@NotEmpty(message = "Please, insert a first name")
private String firstName;
@NotEmpty(message = "Please, insert a last name")
private String lastName;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();
public User() {
}
public User(String username, String email, String password, String firstName, String lastName) {
this.username = username;
this.email = email;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
x1米11米1x
@Entity
@Table(name = "wallet")
public class Wallet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotEmpty(message = "Please, insert a wallet name")
private String walletName;
private double initialBalance;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private User user;
public Wallet() {
}
public Wallet(String walletName, double initialBalance) {
this.walletName = walletName;
this.initialBalance = initialBalance;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getWalletName() {
return walletName;
}
public void setWalletName(String walletName) {
this.walletName = walletName;
}
public double getInitialBalance() {
return initialBalance;
}
public void setInitialBalance(double initialBalance) {
this.initialBalance = initialBalance;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Wallet Repository
boolean existsByUserIdAndWalletName(Long userId, String walletName);
2条答案
按热度按时间sigwle7e1#
您需要在查询中包含
userId
:uqdfh47h2#
或
如果提供了用户的id和walletName,它将只检查该用户是否具有该walletName,如果在这种情况下返回值(true或result〉0),则意味着该特定用户的wallet名称已被占用。
https://www.baeldung.com/spring-data-derived-queries#multiple-condition-expressions