-1

I want to register my users with Spring Data JPA. I have classes for this: Users, Authorities, UsersRepository(interface), AuthoritiesRepository(interface) and a controller which I call save method from AutoWired Repositories.

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 AuthoritiesRepository 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";
    }
}

My 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>

I am using Spring-Data-JPA and MySQL. When I add a new user from register page.Id increments twice for example

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

Id increases twice in the authorities table too. I want to increment id values for 1.

This problem causes because I call different save methods in same time(in registerSubmit method) but I must save it together I can't change this behavior.

  • anyone knows? I need help? – Whatever Man Feb 22 '21 at 12:24
  • I am working to solve this problem about 5 hours. I tried already another generation strategies for primary keys. I also implemented a Service for this but don't work. I talk with people on telegram groups about this problem. No one solves this. – Whatever Man Feb 22 '21 at 14:25
  • I solved this problem by giving AUTO_INCREMENT to all tables and making generatedvalue strategy to IDENTITY – Whatever Man Feb 22 '21 at 16:23

1 Answers1

0

Your solution to use AUTO_INCREMENT instead of AUTO is a good one, because it also avoids the inefficiency of using TABLE which Hibernate defaults to for MySQL.

You would want to stick to table or sequence based ids you'd need to provide a dedicated sequence for each table that has such an id, because by default they use all the same sequence.

On the other hand you shouldn't really care about the continuity of sequence values, because you will have gaps as soon as a rollback occurs anyway and avoiding gaps creates a bottleneck on the application.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348