persist does not throw any exception in a JUnit test
635546May 30 2013 — edited May 30 2013I am implementing a JUnit test using Toplink as JPA provider. I must be missing something because I try to persist two times the same entity and no exception is thrown. Neither PersistenceException nor any other type of exception. The code cannot be easier:
@Test
public void testAddExistingTeam() throws Exception {
Team team = new Team("team2");
try{
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("fofo");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(team);
em.persist(team);
em.getTransaction().commit();
em.close();
}
catch(Exception e){
e.printStackTrace();
}
}
Notice the two em.persist(team).
This code does not seem to either enter the catch block or produce any sort of exception. On the other hand, I have checked that after the first
em.persist(team); the team is really managed.
The relevant parts of the Team class definition follow:
@Entity
@Table (name ="TEAM")
public class Team implements Serializable {
@Id
@Column (name="NAME")
private String name;
@ManyToOne
@JoinColumn (name="CLUB_NAME", referencedColumnName="NAME")
private Club club;
private Category category;
private String email;
@ManyToMany(mappedBy="teams")
private List<Competition> competitions;
public Team (String name){
this.name = name;
this.club = null;
this.competitions = new ArrayList<Competition>();
}
....getters/setters....and more constructors.
}
I am really puzzled by this issue. Somebody could help??? I would be really grateful!!!
Josepma