【发布时间】:2016-10-22 13:29:05
【问题描述】:
我正在使用休眠执行 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 并使用序列插入它们
【问题讨论】:
-
您使用的是什么版本的 Oracle?
-
我使用的是 oracle 11g
-
如果你想使用一个序列来生成 id,你必须在你的注解中配置它 (GenerationType.SEQUENCE)。 Oracle 不支持标识列,因此您当前的 id 生成配置无效。
-
另外,将 MySQLDialect 与 Oracle 一起使用没有什么意义。使用适当的方言。
标签: java oracle hibernate orm hibernate-mapping