我正在使用休眠模式执行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;
}
休眠属性,例如
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");
我的印象是休眠将自动为我生成ID并使用序列插入它们
一些错误:
org.hibernate.dialect.Oracle10gDialect
(11g没有特定的方言)。Integer
,not int
)IDENTITY
仅从版本12c开始,Oracle就支持ID生成
您应该使用其他策略,例如使用序列。
映射如下所示:
@Id
@SequenceGenerator(name = "generator", sequenceName = "ID_SEQUENCE", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
@Column(name = "ID")
private Integer id;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句