Ray
Ray

Reputation: 21

Writing multiple constraints using @Column annotation using spring data JPA

When I am giving multiple constraints separated by "," only the one written first is working. So for email, when I write @Column(nullable=false,unique =true) the first one i.e not null column only works. If I write @Column(unique=true,nullable=false) only unique values are being accepted but null values condition does not work. How do I resolve this?

Entity class

package com.org.io.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="users")
public class UserEntity implements Serializable {
//field we will be storing in our database
    private static final long serialVersionUID = -6757153413054211932L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    
    @Column(nullable=false,length=50)
    private String userId;
    
    @Column(nullable=false,length=50)
    private String name;
    
    @Column(nullable=false,length=150,unique=true)
    private String email;
    
    @Column(nullable=false,length=50)
    private String city;
    
    @Column(nullable=false,length=250)
    private String address;
    
    @Column(nullable=false,length=10)
    private String phoneNumber;
    
    @Column(nullable=false)
    private String encryptedPassword;
    
    private String emailVerificationToken;
    
    @Column(nullable=false)
    private Boolean emailVerificationStatus=false;
    
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }
    
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    
    public String getEncryptedPassword() {
        return encryptedPassword;
    }

    public void setEncryptedPassword(String encryptedPassword) {
        this.encryptedPassword = encryptedPassword;
    }

    public String getEmailVerificationToken() {
        return emailVerificationToken;
    }

    public void setEmailVerificationToken(String emailVerificationToken) {
        this.emailVerificationToken = emailVerificationToken;
    }

    public Boolean getEmailVerificationStatus() {
        return emailVerificationStatus;
    }

    public void setEmailVerificationStatus(Boolean emailVerificationStatus) {
        this.emailVerificationStatus = emailVerificationStatus;
    }


}

application.properties

server.port=9000

#database configuration:mysql

spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#Hibernate config

spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false 

Upvotes: 0

Views: 700

Answers (1)

Ausgefuchster
Ausgefuchster

Reputation: 1178

I think that the problem is that you have getter and setter and therefore have to add the annotation over them aswell or maybe just only over them.

But in general maybe you take a look at spring-boot-starter-validation.

It has some very useful annotations for validations and you get a pretty good exception with detailed information and customizable error messages.

You can e.g. use NotNull or even NotBlank or for the email there is Email.

And another thing I did notice is that you have emailVerificationStatus as Boolean but you initialize it to false so maybe if you don't need it to be null you can use boolean which can't be null and has false as default value.

Upvotes: 0

Related Questions