Why does the below code not enforce unique restrictions?
Person
@Entity @Unique(members = {"name", "surname"}) public class Person implements Serializable { @Id @GeneratedValue private Integer id; private String name; private String surname; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurame(String surname) { this.surname= surname; } }
PersonDao
public class PersonDao { public void create(Person person, ServletContext servletContext) { EntityManagerFactory emf = (EntityManagerFactory) servletContext.getAttribute("emf"); EntityManager em = emf.createEntityManager(); try { em.getTransaction().begin(); em.persist(person); em.getTransaction().commit(); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } } }
Main
public static void main(String[] args) { Person p1 = new Person("John", "Doe"); Person p2 = new Person("John", "Doe"); Person p3 = new Person("John", "Doe"); PersonDao personDao = new PersonDao(); personDao.create(p1); personDao.create(p2); personDao.create(p3); }