Java Spring实体插入“ JDBCSQLINTEGRITYCONSTERTENTERATERATIONEXCEPTION”
我认为这是一个非常简单的问题。但是我看不到。有人可以帮我吗? 我有一个实体“用户”:
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来
选择Hibernate_sequence的下一个值
未触发。在您的情况下,它总是1。
looks like
select next value for hibernate_sequence
not triggered. in your case its always 1 is getting.
感谢您的帮助。
我找到了答案:
做正确的工作。
Thanks for your help.
I found the answer:
does the right job.
您的代码中有一些尝试插入[1,'','tobias','333a4548-5fd6-463e-8d33-657c5174c5a4',dobberphul'],它已经存在。
这就是你的错误
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