Home > Enterprise >  Java Spring Entity insert throws "JdbcSQLIntegrityConstraintViolationException"
Java Spring Entity insert throws "JdbcSQLIntegrityConstraintViolationException"

Time:06-01

I think its a very simple problem. But I dont see it. Can someone please help me? I have an entity "User":

@Entity
public class User {

    @Id
    @GeneratedValue
    private Long id;
    private String firstname;
    private String lastname;
    @Column(unique=true)
    private String email;
    @Column(columnDefinition = "varchar(63) default null", unique = true, nullable = true)
    private String keycloakUserSubject;
    
    @OneToMany(mappedBy = "user")
    private Set<UserProjectRoleOfUserInProject_Assignation> userProjectRoleOfUserInProject_Assignation = new HashSet<>();

//getter and setter...
}

And I have an endpoint "UserController" that tries to insert a new User:

@Service
public class UserController implements UsersApiDelegate {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseEntity<Void> createUser(Optional<String> acceptLanguage) {
        User user = new User();
        user.setId(5L); // I usually tried without this line (cause it is generated) --> same error)
        user.setLastname("st");
        user.setFirstname("te");
        user.setEmail(RandomStringUtils.random(10, true, false) "@mail.com");
        System.out.println(user);
        userRepository.saveAndFlush(user);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

//...
}

Its corresponding userRepository:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findAll();
    Optional<User> findByKeycloakUserSubject(String keycloakUserSubject);
}

Calling that endpoint I get this error:

User [[email protected], firstname=te, id=5, keycloakUserSubject=null, lastname=st, userProjectRoleOfUserInProject_Assignation=[]]
2022-06-01 08:23:47.937  WARN 16560 --- [nio-8000-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23505, SQLState: 23505
2022-06-01 08:23:47.938 ERROR 16560 --- [nio-8000-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper   : Eindeutiger Index oder Primõrschl³ssel verletzt: "PRIMARY KEY ON PUBLIC.USER(ID) [1, '[email protected]', 'Tobias', '333a4548-5fd6-463e-8d33-657c5174c5a4', 'Dobberphul']"
Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.USER(ID) [1, '[email protected]', 'Tobias', '333a4548-5fd6-463e-8d33-657c5174c5a4', 'Dobberphul']"; SQL statement:
insert into user (email, firstname, keycloak_user_subject, lastname, id) values (?, ?, ?, ?, ?) [23505-200]2022-06-01 08:23:47.953 ERROR 16560 --- [nio-8000-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["PRIMARY KEY ON PUBLIC.USER(ID) [1, '[email protected]', 'Tobias', '333a4548-5fd6-463e-8d33-657c5174c5a4', 'Dobberphul']"; SQL statement:
insert into user (email, firstname, keycloak_user_subject, lastname, id) values (?, ?, ?, ?, ?) [23505-200]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Eindeutiger Index oder Primõrschl³ssel verletzt: 
"PRIMARY KEY ON PUBLIC.USER(ID) [1, '[email protected]', 'Tobias', '333a4548-5fd6-463e-8d33-657c5174c5a4', 'Dobberphul']"
Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.USER(ID) [1, '[email protected]', 'Tobias', '333a4548-5fd6-463e-8d33-657c5174c5a4', 'Dobberphul']"; SQL statement:
insert into user (email, firstname, keycloak_user_subject, lastname, id) values (?, ?, ?, ?, ?)

I think I get blind sometimes... Thanks a lot.

CodePudding user response:

There is something in your code who try to insert [1, '[email protected]', 'Tobias', '333a4548-5fd6-463e-8d33-657c5174c5a4', 'Dobberphul'] and it already exist. This is what your error say

CodePudding user response:

looks like select next value for hibernate_sequence not triggered. in your case its always 1 is getting.

CodePudding user response:

Thanks for your help.

I found the answer:

@GeneratedValue(strategy = GenerationType.IDENTITY)

does the right job.

  • Related