Spring Boot 无法添加表行,因为出现“当IDENTITY_INSERT设置为OFF时,无法为表'Users'中的标识列插入显式值”错误

cu6pst1q  于 2022-11-05  发布在  Spring
关注(0)|答案(2)|浏览(470)

我尝试使用Sping Boot 和JPA将用户保存到数据库,但由于此错误,它对我不起作用。

  1. com.microsoft.sqlserver.jdbc.SQLServerException:
  2. Cannot insert explicit value for identity column in table 'Users'
  3. when IDENTITY_INSERT is set to OFF.
  • 错误 * 很清楚,所以我尝试在我的数据库中更改Identity插入,成功了,但错误仍然出现。第二个奇怪的问题是,我没有尝试将特定值作为id放入我的数据库。下面是负责保存数据的确切行:
  1. userService.save(new User("wikimmax","test","testmail@op.pl","test", Arrays.asList(new Role("ROLE_USER"))));

这是我的User类

  1. import javax.persistence.*;
  2. import java.util.Collection;
  3. @Entity
  4. @Table(name = "Users",uniqueConstraints = @UniqueConstraint(columnNames = "email"))
  5. public class User {
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.AUTO)
  8. @Column(name = "id")
  9. private Long id;
  10. private String firstName;
  11. private String lastName;
  12. private String email;
  13. private String password;
  14. @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  15. @JoinTable(
  16. name = "users_roles",
  17. joinColumns = @JoinColumn(
  18. name = "user_id", referencedColumnName = "id"),
  19. inverseJoinColumns = @JoinColumn(
  20. name = "role_id", referencedColumnName = "id"))
  21. private Collection<Role> roles;
  22. public User() {
  23. }
  24. public User(String firstName, String lastName, String email, String password) {
  25. this.firstName = firstName;
  26. this.lastName = lastName;
  27. this.email = email;
  28. this.password = password;
  29. }
  30. public User(String firstName, String lastName, String email, String password, Collection<Role> roles) {
  31. this.firstName = firstName;
  32. this.lastName = lastName;
  33. this.email = email;
  34. this.password = password;
  35. this.roles = roles;
  36. }
  37. public Long getId() {
  38. return id;
  39. }
  40. public void setId(Long id) {
  41. this.id = id;
  42. }
  43. public String getFirstName() {
  44. return firstName;
  45. }
  46. public void setFirstName(String firstName) {
  47. this.firstName = firstName;
  48. }
  49. public String getLastName() {
  50. return lastName;
  51. }
  52. public void setLastName(String lastName) {
  53. this.lastName = lastName;
  54. }
  55. public String getEmail() {
  56. return email;
  57. }
  58. public void setEmail(String email) {
  59. this.email = email;
  60. }
  61. public String getPassword() {
  62. return password;
  63. }
  64. public void setPassword(String password) {
  65. this.password = password;
  66. }
  67. public Collection<Role> getRoles() {
  68. return roles;
  69. }
  70. public void setRoles(Collection<Role> roles) {
  71. this.roles = roles;
  72. }
  73. @Override
  74. public String toString() {
  75. return "User{" +
  76. "id=" + id +
  77. ", firstName='" + firstName + '\'' +
  78. ", lastName='" + lastName + '\'' +
  79. ", email='" + email + '\'' +
  80. ", password='" + "*********" + '\'' +
  81. ", roles=" + roles +
  82. '}';
  83. }
  84. }

还有Is my表属性:

任何想法都是高度赞赏

UPDATE我设法打印了由Hibernate生成SQL查询,它看起来像这样:

  1. insert
  2. into
  3. Users
  4. (email, firstName, lastName, password, id)
  5. values
  6. (?, ?, ?, ?, ?)
bjg7j2ky

bjg7j2ky1#

请编辑问题并添加插入语句。
您不能在标识列中插入任何数据,因为此列由SQL引擎自动填充,请在Insert语句中删除id列,然后重试。

设置标识,但这不是个好主意。允许将显式值插入表的标识列。

  1. SET IDENTITY_INSERT Users ON

例如:

  1. USE AdventureWorks2012;
  2. GO
  3. -- Create tool table.
  4. CREATE TABLE dbo.Tool(
  5. ID INT IDENTITY NOT NULL PRIMARY KEY,
  6. Name VARCHAR(40) NOT NULL
  7. );
  8. GO
  9. -- Inserting values into products table.
  10. INSERT INTO dbo.Tool(Name)
  11. VALUES ('Screwdriver')
  12. , ('Hammer')
  13. , ('Saw')
  14. , ('Shovel');
  15. GO
  16. -- Create a gap in the identity values.
  17. DELETE dbo.Tool
  18. WHERE Name = 'Saw';
  19. GO
  20. SELECT *
  21. FROM dbo.Tool;
  22. GO
  23. -- Try to insert an explicit ID value of 3;
  24. -- should return an error:
  25. -- An explicit value for the identity column in table 'AdventureWorks2012.dbo.Tool' can only be specified when a column list is used and IDENTITY_INSERT is ON.
  26. INSERT INTO dbo.Tool (ID, Name) VALUES (3, 'Garden shovel');
  27. GO
  28. -- SET IDENTITY_INSERT to ON.
  29. SET IDENTITY_INSERT dbo.Tool ON;
  30. GO
  31. -- Try to insert an explicit ID value of 3.
  32. INSERT INTO dbo.Tool (ID, Name) VALUES (3, 'Garden shovel');
  33. GO
  34. SELECT *
  35. FROM dbo.Tool;
  36. GO
  37. -- Drop products table.
  38. DROP TABLE dbo.Tool;
  39. GO
展开查看全部
gr8qqesn

gr8qqesn2#

添加以下代码:

  1. @Id
  2. @Column(name = "id", nullable = false)
  3. @GeneratedValue(strategy = GenerationType.IDENTITY)
  4. private Long id;

相关问题