Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoIncrement Id PostgreSQL and Spring Boot Data JPA

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.

like image 270
andresscode Avatar asked Jan 22 '17 14:01

andresscode


People also ask

How to reset auto-increment row ID in a Postgres database?

In Postgresql, to reset the auto-increment row ID in a Postgres database, here is a command TRUNCATE that removes all data and reset auto increment to 0. Use the below command. Where <TABLENAME> is the name of the table. Let’s create the table quickly and insert some records into it.

What is PostgreSQL auto increment?

In this Postgresql tutorial, we will learn about “Postgresql auto increment” using different auto-increment data types of Postgresql that create a unique identifier for each record in a column. Before beginning, we need to know, In Postgresql to create the auto-increment, we use data types that are smallserial, serial, and bigserial.

How do I use PostgreSQL in Spring Boot?

Configuring PostgreSQL Let’s configure Spring Boot to use PostgreSQL as our data source. You can do that simply by adding PostgreSQL database URL, username, and password in the src/main/resources/application.properties file - 7. Create JPA Entity - Employee.java

How to generate the ID of a record using JPA?

As the name suggests, the database creates a separate table and the data JPA will insert the value for the table also uses the same value to insert the record into the database table. We can also use a Custom generator to generate the ID instead of using built-in strategies.


1 Answers

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;
  }
}
like image 144
andresscode Avatar answered Oct 13 '22 07:10

andresscode