Using the Java Persistence with Hibernate book "Hello World" example from chapter 2 as a guide, I created an Employee class with an Id field that is annotated as follows:
@Id @GeneratedValue
@Column(name = "EMPLOYEE_ID")
private Long _id;
From main, I create an Employee instance and call the method below to insert it to the MySql DB. It is inserted with EMPLOYEE_ID= 1. Then I create another Employee instance and call the same method below. This time it appears to update the first record with the field values of the new Employee, rather than insert a new record with EMPLOYEE_ID= 2, which is what I expected. So only one record is in the DB, even thought I called the method below twice. It appears that it is not auto incrementing the ID. Can anyone help?
public boolean insertEmployee(Employee inEmployee)
{
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("employee");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(inEmployee);
tx.commit();
// Shutting down the application service
em.close();
return true;
}
|