AutoIncrement Id PostgreSQL and Spring Boot Data JPA

34,546

I found the solution. I need to change the script for these one:

CREATE TABLE users(
    id  SERIAL PRIMARY KEY NOT NULL,
    email   TEXT NOT NULL,
    password    TEXT    NOT NULL
);

Then, the Entity should be annotated with this:

@Entity
@Table(name = "users")
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(columnDefinition = "serial")
  private Long id;
  private String email;
  private String password;

  public User() {}

  public Long getId() {
    return id;
  }

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

  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;
  }
}
Share:
34,546
andresscode
Author by

andresscode

Software Developer with strong knowledge of Java, Spring, Object-Oriented Programming, Object-Oriented Design (SOLID Principles), Web Services (RESTful and SOAP), Android Development (Android Studio), Web Development (JavaScript, HTML, CSS, Bootstrap, PHP, and Laravel), Salesforce Development (Apex, Visualforce and Lightning Components), Databases (MySQL, SOQL, and MongoDB), Version Control Systems (Git and Github), and SaaS (Heroku and Google Firebase). English Fluent. Interested in algorithms and data structures, blockchain and decentralized apps, OOP, AI, video games, OOD, and computing performance. I am a passionate developer with a kind attitude to face programming challenges.

Updated on March 07, 2020

Comments

  • andresscode
    andresscode over 4 years

    I'm having problems trying to create a new record in my PostgreSQL database. I just want to POST to the REST service a new user (int:id, String:email, String:password) but, I'm having this error:

    "exception": "org.springframework.dao.DataIntegrityViolationException",
    "message": "could not execute statement; SQL [n/a]; constraint [id]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
    

    These are my Java classes:

    Domain

    @Entity
    @Table(name = "users")
    public class User {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;
      private String email;
      private String password;
    
      public User() {}
    
      public Integer getId() {
        return id;
      }
    
      public void setId(Integer id) {
        this.id = id;
      }
    
      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;
      }
    }
    

    Controller

    @RestController
    @RequestMapping("/users")
    public class UserController {
      @Autowired
      private UserService userService;
    
      @RequestMapping(method = RequestMethod.GET)
      public List<User> findAll() {
        return userService.findAll();
      }
    
      @RequestMapping(method = RequestMethod.POST)
      public User addUser(@RequestBody User user) {
        userService.addUser(user);
        return user;
      }
    }
    

    Service

    @Service
    public class UserService {
      @Autowired
      private UserRepository userRepository;
    
      public List<User> findAll() {
        return (List<User>) userRepository.findAll();
      }
    
      public User addUser(User user) {
        userRepository.save(user);
        return user;
      }
    }
    

    Repository

    public interface UserRepository extends CrudRepository<User, Integer> {
      // TODO
    }
    

    SQL

    CREATE TABLE users(
        id INT PRIMARY KEY NOT NULL,
        email   TEXT NOT NULL,
        password    CHAR(20)    NOT NULL
    );
    

    Please, somebody help me, because I don't know how to tackle this issue.