Java Spring实体插入“ JDBCSQLINTEGRITYCONSTERTENTERATERATIONEXCEPTION”

发布于 2025-02-03 05:39:06 字数 4404 浏览 3 评论 0原文

我认为这是一个非常简单的问题。但是我看不到。有人可以帮我吗? 我有一个实体“用户”:

@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...
}

我有一个端点“ usercontroller”,它试图插入一个新用户:

@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);
    }

//...
}

其相应的用户repository:

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

呼叫该端点,我会遇到此错误:

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阿楠 2025-02-10 05:39:07

看起来选择Hibernate_sequence的下一个值
未触发。在您的情况下,它总是1。

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

坐在坟头思考人生 2025-02-10 05:39:07

感谢您的帮助。

我找到了答案:

@GeneratedValue(strategy = GenerationType.IDENTITY)

做正确的工作。

Thanks for your help.

I found the answer:

@GeneratedValue(strategy = GenerationType.IDENTITY)

does the right job.

水晶透心 2025-02-10 05:39:06

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文