Home > Net >  How to check if username already exists in table?
How to check if username already exists in table?

Time:01-18

I have REST api app and when I create a user with the same username it creates all the time, but I want username to be unique cause in real apps username is unique for each user, how can I provide checking username in db? I want my app to throw some message when i create user with username that already exists,and of course do not save new user username that already exists

My user Entity:

@Table(name = "usr", uniqueConstraints=
@UniqueConstraint(columnNames={"username", "email"}))
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

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

    @Column(name = "last_name")
    private String lastName;

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

    @Column(name = "email")
    private String email;

    @Column(name = "status")
    private Status status;

//    @OneToMany(mappedBy = "author")
//    private List<Message> messages;

//    @OneToMany(mappedBy = "author")
//    private List<Comment> comments;

    @OneToOne
    @JoinColumn(name = "gender_id")
    private Gender gender;

    @OneToOne
    @JoinColumn(name = "address_id")
    private Address address;

    @ManyToMany
    @JoinTable(
            name = "user_subscriptions",
            joinColumns =  @JoinColumn(name = "channel_id") ,
            inverseJoinColumns =  @JoinColumn(name = "subscriber_id"))
    private Set<User> subscribers;

    @ManyToMany
    @JoinTable(
            name = "user_subscriptions",
            joinColumns =  @JoinColumn(name = "subscriber_id") ,
            inverseJoinColumns =  @JoinColumn(name = "channel_id"))
    private Set<User> subscriptions;

    @OneToOne
    @JoinColumn(name = "file_id")
    private FileEntity fileEntity;

My user service:

@Transactional
    public User save(UserRequest user) {
        provideAllUserCheckingActionsForSave(user);
        User userUntilSave = userMap.userRequestToUser(user,Role.USER,Status.ACTIVE);
        userUntilSave.setPassword(passwordEncoder.encode(userUntilSave.getPassword()));
        User saved = userRepo.save(userUntilSave);
        log.info("User save method invoked");
        return saved;
    }

CodePudding user response:

You can add validation to the controller or you can simply do it as follows:- When a user wants to register, get the username and search in the database whether it already exists or not. If findByUsername(username) returns null then perform the next operation and register the user, else return some error message.

CodePudding user response:

You can use @unique annotation to store the unique values in the database and if it throws the exception for not taking the unique value the exception should be handled.

CodePudding user response:

Before create user,you can search user by username,if search result is empty,then create new user,if search result is not empty,deal the question.

  •  Tags:  
  • Related