I have some code
@Entity
@Table(name = "USER")
public class User {
@Id
@Column(name = "id", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
When I try to add user to table, I catch
org.h2.jdbc.JdbcSQLException: NULL not allowed for column "ID"; SQL statement:
insert into USER (id, birthday, email, first_name, last_name, login, password, id_role) values (null, ?, ?, ?, ?, ?, ?, ?) [23502-181]
This is my code to add user to db
stock2.setBirthday(new Date(46));
stock2.setEmail("sss");
stock2.setFirstName("oleg");
stock2.setId(506l);
stock2.setLastName("gubov");
stock2.setLogin("OP");
stock2.setPassword("1");
Role role2 = new Role();
role2.setName("poil");
role2.setId(7l);
stock2.setRole(role2);
HibernateUserDao dao = new HibernateUserDao();
System.out.println(stock2.getId() + " -----------before");
dao.create(stock2);
and code of create method:
public void create(User user) {
Session session = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
throw e;
} finally {
if (session != null && session.isOpen()) {
session.close();
}
}
}
In my opinion, id is 7,but not NULL. And "java" thinks otherwise. Where can be the problem?