HHH-17772 exception mappings for lifecycle methods

This commit is contained in:
Gavin King 2024-02-23 22:26:30 +01:00
parent 9c707dd4e8
commit 0cf8d18c3d
1 changed files with 24 additions and 2 deletions

View File

@ -52,20 +52,42 @@ public class LifecycleMethod implements MetaAttribute {
.append(methodName) .append(methodName)
.append('('); .append('(');
notNull( declaration ); notNull( declaration );
final boolean isInsert = operationName.equals("insert");
declaration declaration
.append(annotationMetaEntity.importType(entity)) .append(annotationMetaEntity.importType(entity))
.append(' ') .append(' ')
.append(parameterName) .append(parameterName)
.append(')') .append(')')
.append(" {\n") .append(" {\n")
.append("\t") .append("\ttry {\n")
.append("\t\t")
.append(sessionName) .append(sessionName)
.append('.') .append('.')
.append(operationName) .append(operationName)
.append('(') .append('(')
.append(parameterName) .append(parameterName)
.append(')') .append(')')
.append(";\n}"); .append(";\n")
.append("\t}\n")
.append("\tcatch (")
.append(annotationMetaEntity.importType(isInsert
? "jakarta.persistence.EntityExistsException"
: "jakarta.persistence.OptimisticLockException"))
.append(" exception) {\n")
.append("\t\tthrow new ")
.append(annotationMetaEntity.importType(isInsert
? "jakarta.data.exceptions.EntityExistsException"
: "jakarta.data.exceptions.OptimisticLockingFailureException"))
.append("(exception);\n")
.append("\t}\n")
.append("\tcatch (")
.append(annotationMetaEntity.importType("jakarta.persistence.PersistenceException"))
.append(" exception) {\n")
.append("\t\tthrow new ")
.append(annotationMetaEntity.importType("jakarta.data.exceptions.DataException"))
.append("(exception);\n")
.append("\t}\n")
.append("}");
return declaration.toString(); return declaration.toString();
} }