( Hibernate Oracle 无法插入 NULL ) 如何插入自动生成的 ID?
( Hibernate Oracle cannot insert NULL ) How to insert auto generated ID?
我正在从事使用休眠执行 CRUD 操作的项目。我有用户模型,我正在尝试插入信息但一直收到此错误
Hibernate: insert into APPUSER (dob, email, firstName, lastName, password) values (?, ?, ?, ?, ?)
Jun 21, 2016 2:17:07 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1400, SQLState: 23000
Jun 21, 2016 2:17:07 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-01400: cannot insert NULL into ("MYAPP8785"."APPUSER"."ID")
用户模型看起来像
@Entity
@Table(name="APPUSER")
public class AppUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Email
@Size(max = 50)
private String email;
@Column
private String dob;
@Column
private String firstName;
@Column
private String lastName;
@Column(name = "password", nullable = false)
private String password;
}
类似
的 Hibernate 属性
properties.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
//properties.put("hibernate.current_session_context_class","thread");
properties.put("hibernate.hbm2ddl.auto","update");
properties.put("hibernate.show_sql","true");
我的印象是 hibernate 会自动为我生成 id 并使用序列插入它们
您需要创建 table APPUSER
使其主键 ID
具有自动递增功能。请按照 link How to create id with AUTO_INCREMENT on Oracle? 如何操作。只有这样你才会看到 hibernate 会为你在主键字段中插入值。
一些错误:
- 使用正确的方言:
org.hibernate.dialect.Oracle10gDialect
(11g 没有特定的方言)。
- 不要为休眠生成的 ID 使用原始类型(使用
Integer
,而不是 int
)
IDENTITY
用于 ID 生成仅受 oracle 支持,因为 version 12c
你应该使用其他策略,例如使用序列。
映射看起来像:
@Id
@SequenceGenerator(name = "generator", sequenceName = "ID_SEQUENCE", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
@Column(name = "ID")
private Integer id;
我看到您找到了一个可用的答案,但由于我遇到了同样的问题并且在搜索解决方案时遇到了这个 post,所以我将包括对我有用的答案。我正在使用 Oracle 12c——这仅适用于 12c——解决方案是将方言更改为 Oracle12cDialect。否则,只需使用
@GeneratedValue(strategy = GenerationType.IDENTITY)
成功了。
我正在从事使用休眠执行 CRUD 操作的项目。我有用户模型,我正在尝试插入信息但一直收到此错误
Hibernate: insert into APPUSER (dob, email, firstName, lastName, password) values (?, ?, ?, ?, ?)
Jun 21, 2016 2:17:07 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1400, SQLState: 23000
Jun 21, 2016 2:17:07 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-01400: cannot insert NULL into ("MYAPP8785"."APPUSER"."ID")
用户模型看起来像
@Entity
@Table(name="APPUSER")
public class AppUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Email
@Size(max = 50)
private String email;
@Column
private String dob;
@Column
private String firstName;
@Column
private String lastName;
@Column(name = "password", nullable = false)
private String password;
}
类似
的 Hibernate 属性properties.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
//properties.put("hibernate.current_session_context_class","thread");
properties.put("hibernate.hbm2ddl.auto","update");
properties.put("hibernate.show_sql","true");
我的印象是 hibernate 会自动为我生成 id 并使用序列插入它们
您需要创建 table APPUSER
使其主键 ID
具有自动递增功能。请按照 link How to create id with AUTO_INCREMENT on Oracle? 如何操作。只有这样你才会看到 hibernate 会为你在主键字段中插入值。
一些错误:
- 使用正确的方言:
org.hibernate.dialect.Oracle10gDialect
(11g 没有特定的方言)。 - 不要为休眠生成的 ID 使用原始类型(使用
Integer
,而不是int
)
IDENTITY
用于 ID 生成仅受 oracle 支持,因为 version 12c
你应该使用其他策略,例如使用序列。
映射看起来像:
@Id
@SequenceGenerator(name = "generator", sequenceName = "ID_SEQUENCE", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
@Column(name = "ID")
private Integer id;
我看到您找到了一个可用的答案,但由于我遇到了同样的问题并且在搜索解决方案时遇到了这个 post,所以我将包括对我有用的答案。我正在使用 Oracle 12c——这仅适用于 12c——解决方案是将方言更改为 Oracle12cDialect。否则,只需使用
@GeneratedValue(strategy = GenerationType.IDENTITY)
成功了。