Spring Data JPA:当同时调用两个存储库保存方法时,Id增加两次



我想用Spring Data JPA注册我的用户。我有这样的类:用户,权限,UsersRepository(接口),authortiesrepository(接口)和一个控制器,我从AutoWired存储库调用保存方法。

My Users Class:

@Entity
public class Users {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String username;
private String password;
private int enabled = 1;
private static Users instance = null;
public static Users getInstance() {
if (instance == null) {
instance = new Users();
}
return instance;
}
protected Users() {}
public Users(String username, String password, int enabled) {
this.username = username;
this.password = password;
this.enabled = enabled;
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}

public String getPassword() {
return password;
}
public int getEnabled() {
return enabled;
}
}

My Authorities Class:

@Entity
public class Authorities {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String username;
private String authority;
protected Authorities() { }
public Authorities(String username, String authority) {
this.username = username;
this.authority = authority;
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getAuthority() {
return authority;
}
}

My UsersRepository Interface

public interface UsersRepository extends CrudRepository<Users, Integer> {
}

My authortiesrepository Interface

public interface AuthoritiesRepository extends CrudRepository<Authorities, Integer> {
}

My Controller Class

@Controller
public class MyController {
@Autowired
private UsersRepository usersRepository;
@Autowired
private AuthoritiesRepository authoritiesRepository;
@Autowired
private ArticleRepository articleRepository;
@GetMapping("/register")
public String register(Model model) {
model.addAttribute("users", Users.getInstance());
return "register";
}
@PostMapping("/register")
public String registerSubmit(@ModelAttribute Users users) {
usersRepository.save(users);
Authorities authority = new Authorities(users.getUsername(), "read");
AuthoritiesRepository.save(authority);
return "redirect:/login";
}
}

我register.html

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h1>Register:</h1>
<form action="#" th:action="@{/register}" th:object="${users}" method="post">
<p>Username: <input type="text" th:field="*{username}" /></p>
<p>Password: <input type="password" th:field="*{password}" /></p>
<input type="hidden" th:field="*{enabled}" />
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>

我使用Spring-Data-JPA和MySQL。当我从注册页面添加新用户时。Id增加两次,例如

Id:3 Username:user1 Password:pass1
Id:5 Username:user2 Password:pass2
İd:7 Username:user3 Password:pass3

权威表中的Id也增加两次。我想为1增加id值

这个问题是因为我调用不同的保存方法在同一时间(在registerSubmit方法),但我必须保存在一起,我不能改变这个行为。

您使用AUTO_INCREMENT而不是AUTO的解决方案是一个很好的解决方案,因为它也避免了使用Hibernate默认为MySQL使用的TABLE的低效率。

如果你想坚持基于表或序列的id,你需要为每个有这样一个id的表提供一个专用的序列,因为默认情况下它们使用相同的序列。

另一方面,您不应该真正关心序列值的连续性,因为无论如何,只要回滚发生,就会有间隙,避免间隙会在应用程序上造成瓶颈。

相关内容

  • 没有找到相关文章

最新更新