![](/img/trans.png)
[英]SqlException: Cannot insert explicit value for identity column in table [Table name] when IDENTITY_INSERT is set to OFF
[英]Cannot add a table row because of "Cannot insert explicit value for identity column in table 'Users' when IDENTITY_INSERT is set to OFF" error
我正在嘗試使用 Spring boot 和 JPA 將用戶保存到數據庫中,但由於此錯誤,它對我不起作用。
com.microsoft.sqlserver.jdbc.SQLServerException:
Cannot insert explicit value for identity column in table 'Users'
when IDENTITY_INSERT is set to OFF.
錯誤很明顯,所以我嘗試在我的數據庫中更改身份插入成功,但錯誤仍然發生。 在那個問題中很奇怪的第二件事是我沒有嘗試將特定值作為 id 放入我的數據庫。 這是負責保存數據的確切行:
userService.save(new User("wikimmax","test","testmail@op.pl","test", Arrays.asList(new Role("ROLE_USER"))));
還有我的 User 類
import javax.persistence.*;
import java.util.Collection;
@Entity
@Table(name = "Users",uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
private String firstName;
private String lastName;
private String email;
private String password;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(
name = "users_roles",
joinColumns = @JoinColumn(
name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(
name = "role_id", referencedColumnName = "id"))
private Collection<Role> roles;
public User() {
}
public User(String firstName, String lastName, String email, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
}
public User(String firstName, String lastName, String email, String password, Collection<Role> roles) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
this.roles = roles;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
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;
}
public Collection<Role> getRoles() {
return roles;
}
public void setRoles(Collection<Role> roles) {
this.roles = roles;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", password='" + "*********" + '\'' +
", roles=" + roles +
'}';
}
}
有我的桌子
任何想法都受到高度贊賞
更新我設法打印了由休眠生成的 SQL 查詢,它看起來像這樣:
insert
into
Users
(email, firstName, lastName, password, id)
values
(?, ?, ?, ?, ?)
請編輯問題並添加插入語句。
您不能在身份列中插入任何數據,因為該列由 SQL 引擎自動填充,在Insert
語句中刪除 id 列並重試。
或者設置身份,但這不是個好主意。 允許將顯式值插入到表的標識列中。
SET IDENTITY_INSERT Users ON
例如 :
USE AdventureWorks2012;
GO
-- Create tool table.
CREATE TABLE dbo.Tool(
ID INT IDENTITY NOT NULL PRIMARY KEY,
Name VARCHAR(40) NOT NULL
);
GO
-- Inserting values into products table.
INSERT INTO dbo.Tool(Name)
VALUES ('Screwdriver')
, ('Hammer')
, ('Saw')
, ('Shovel');
GO
-- Create a gap in the identity values.
DELETE dbo.Tool
WHERE Name = 'Saw';
GO
SELECT *
FROM dbo.Tool;
GO
-- Try to insert an explicit ID value of 3;
-- should return an error:
-- 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.
INSERT INTO dbo.Tool (ID, Name) VALUES (3, 'Garden shovel');
GO
-- SET IDENTITY_INSERT to ON.
SET IDENTITY_INSERT dbo.Tool ON;
GO
-- Try to insert an explicit ID value of 3.
INSERT INTO dbo.Tool (ID, Name) VALUES (3, 'Garden shovel');
GO
SELECT *
FROM dbo.Tool;
GO
-- Drop products table.
DROP TABLE dbo.Tool;
GO
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.