爲什麼JPA在我執行合併的方法中爲唯一約束違規引發異常?相反,我看到在我的代碼中拋出一個異常。JPA異常合併
我想獲取異常消息(更新錯誤),但它沒有捕獲異常。
我希望得到這樣的響應:
{
"errorMessage": "Update Error",
"errorCode": 404,
"documentation": ""
}
相反,在控制檯中我得到這個錯誤:
Caused by: javax.ejb.EJBTransactionRolledbackException: Transaction rolled back
.....
...
Caused by: javax.transaction.RollbackException: ARJUNA016053: Could not commit transaction.
.....
.....
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
......
.....
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (DEMAND_TD_CODE) violated
.....
....
這裏是我做了更新:
@Override
public Demand updateDemand(Demand demand) throws DemandExceptions {
try {
Demand demandToUpdate = entityManager.merge(demand);
} catch (PersistenceException e) {
throw new DemandExceptions("Update Error");
}
return demandToUpdate;
}
DemandeExceptions .java
public class DemandeExceptions extends Exception implements Serializable {
private static final long serialVersionUID = 1L;
public DemandeExceptions (String message) {
super(message);
}
}
@Provider
public class DemandExceptionsMapper implements ExceptionMapper<DemandeExceptions >{
@Override
public Response toResponse(DemandeExceptions ex) {
ErrorMessage errorMessage = new ErrorMessage(ex.getMessage(), 404, "");
return Response.status(Status.NOT_FOUND)
.entity(errorMessage)
.build();
}
}
ErrorMessage.java
@XmlRootElement
public class ErrorMessage {
private String errorMessage;
private int errorCode;
private String documentation;
..
}
'違反唯一約束(DEMAND_TD_CODE)'。所以修復爲什麼這是違反... –
我在數據庫中定義它像獨特,我試圖添加一個實體witj相同的代碼進行測試 – user1814879
所以這是一個測試,你只是想檢測它?所以尋找一個帶有原因異常的PersistenceException,它是SQLIntegrityConstraintViolationException –