Hibernate: “Field 'id' doesn't have a default value” - This error came up when a simple insert was tried using HIbernate.
session.getTransaction().begin();
User user = new User();
user.setBirthDate(new Date());
user.setCreatedBy("ABC");
user.setCreatedDate(new Timestamp(new Date().getTime()));
user.setEmailAdress("abc@gmail.com");
user.setFirstName("ABC");
user.setLastName("DEF");
user.setLastUpdatedBy("SBC");
user.setLastUpdatedDate(new Timestamp(new Date().getTime()));
session.save(user);
session.getTransaction().commit();
Dropped the table 'User' which was created explicitly user DDL scripts and tried to generate it through HIbernate.
Ran the program after dropping the 'User' table in database.
But,the table did not get created in database.
An error came up. Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'User' doesn't exist.
Resolution: Added a line in hibernate.cfg.xml file
This will create table in database using HIbernate as well insert the above row into the table.
The Generation strategy used in User entity class was
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="USER_ID")
private Long userId;
No comments:
Post a Comment