I have set up a very simple Java web project (using NetBeans' Web Application template). It is deployed on Payara 6.2025.7 with a H2 database as a JTA data source.
I can't get EntityExistsException from persisting the same Entity twice. Instead, I always get jakarta.persistence.PersistenceException. Nested are internal exceptions with details what constraint has been violated.
Here is the code I am using:
@GET
@Path("throwme")
@Transactional
public Response exceptional(){
E e = new E();
e.setId("yolo");
em.persist(e);
return Response
.ok("you can't see me twice!")
.build();
}
just in case it would make any difference, here is the definition of entity E:
@Entity
@Data
public class E {
@Id
@GeneratedValue
private String id;
private int desyncer;
}
(@Data is from lombok)
The expected behavior is that I should get EnityExistsException after second request of \throwme resource. Instead, I get this:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 4.0.1.payara-p4.v202504221851): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation: "PUBLIC.PRIMARY_KEY_45 ON PUBLIC.E(ID) VALUES ( /* 25 */ 'yolo' )"; SQL statement:
It seems my problem is similar to the issue: https://github.com/eclipse-ee4j/eclipselink/issues/2170 but I can't reproduce the solution mentioned in the last post.
UPDATE: While trying to get to the root cause of the problem I have modified the code so it looks like this:
@GET
@Path("throwme")
@Transactional
public Response exceptional(){
try{
E e = new E();
e.setId("yolo");
em.persist(e);
em.flush();
}catch(Exception e){
System.out.println(e.getClass());
}
return Response
.ok("you can't see me!")
.build();
}
Now I am convinced that the root exception is jakarta.persistence.PersistenceException and it is thrown at em.flush()
@Data- > use@Getteror@Setter@Dataprovidesequals()andhashCode()