move some boilerplate exception-throwing into a utility class in core
Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
parent
d90807f9e4
commit
954742b383
|
@ -10,7 +10,7 @@ import org.hibernate.ObjectNotFoundException;
|
|||
import org.hibernate.proxy.EntityNotFoundDelegate;
|
||||
|
||||
/**
|
||||
* Standard non-JPA implementation of EntityNotFoundDelegate, throwing the
|
||||
* Standard non-JPA implementation of {@link EntityNotFoundDelegate}, throwing the
|
||||
* Hibernate-specific {@link ObjectNotFoundException}.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.exception.spi;
|
||||
|
||||
import org.hibernate.Incubating;
|
||||
import org.hibernate.ObjectNotFoundException;
|
||||
|
||||
/**
|
||||
* Utility methods used by Hibernate Processor.
|
||||
*
|
||||
* @author Gavin King
|
||||
*
|
||||
* @since 6.5
|
||||
*/
|
||||
@Incubating
|
||||
public class Exceptions {
|
||||
public static <E> E require(E entity, String entityName, Object id) {
|
||||
if ( entity == null ) {
|
||||
throw new ObjectNotFoundException( entityName, id );
|
||||
}
|
||||
else {
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public static void require(Object argument, String parameterName) {
|
||||
if ( argument == null ) {
|
||||
throw new IllegalArgumentException("Null " + parameterName);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -78,4 +78,15 @@ public abstract class AbstractAnnotatedMethod implements MetaAttribute {
|
|||
return emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
void nullCheck(StringBuilder declaration, String parameterName) {
|
||||
declaration
|
||||
.append('\t')
|
||||
.append(annotationMetaEntity.staticImport("org.hibernate.exception.spi.Exceptions", "require"))
|
||||
.append('(')
|
||||
.append(parameterName.replace('.', '$'))
|
||||
.append(", \"")
|
||||
.append(parameterName)
|
||||
.append("\");\n");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -116,15 +116,6 @@ public abstract class AbstractCriteriaMethod extends AbstractFinderMethod {
|
|||
}
|
||||
}
|
||||
|
||||
private static void nullCheck(StringBuilder declaration, String paramName) {
|
||||
declaration
|
||||
.append("\tif (")
|
||||
.append(paramName.replace('.', '$'))
|
||||
.append(" == null) throw new IllegalArgumentException(\"Null ")
|
||||
.append(paramName)
|
||||
.append("\");\n");
|
||||
}
|
||||
|
||||
void where(StringBuilder declaration, List<String> paramTypes) {
|
||||
declaration
|
||||
.append("\t_query.where(");
|
||||
|
|
|
@ -60,7 +60,7 @@ public class IdFinderMethod extends AbstractFinderMethod {
|
|||
|
||||
@Override
|
||||
boolean singleResult() {
|
||||
return false; // we don't need to convert Query exceptions
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -92,35 +92,11 @@ public class IdFinderMethod extends AbstractFinderMethod {
|
|||
}
|
||||
else if (!nullable) {
|
||||
declaration
|
||||
.append(";\n");
|
||||
if (dataRepository) {
|
||||
declaration
|
||||
.append("\t\tif (_result == null) throw new ")
|
||||
.append(annotationMetaEntity.importType("jakarta.data.exceptions.EmptyResultException"))
|
||||
.append("(\"No '")
|
||||
.append(annotationMetaEntity.importType(entity))
|
||||
.append("' for given id [\" + ")
|
||||
.append(paramName)
|
||||
.append(" + \"]\",\n\t\t\t\tnew ")
|
||||
.append(annotationMetaEntity.importType("org.hibernate.ObjectNotFoundException"))
|
||||
.append("((Object) ")
|
||||
.append(paramName)
|
||||
.append(", \"")
|
||||
.append(entity)
|
||||
.append("\"));\n")
|
||||
.append("\t\treturn _result");
|
||||
}
|
||||
else {
|
||||
declaration
|
||||
.append("\tif (_result == null) throw new ")
|
||||
.append(annotationMetaEntity.importType("org.hibernate.ObjectNotFoundException"))
|
||||
.append("((Object) ")
|
||||
.append(paramName)
|
||||
.append(", \"")
|
||||
.append(entity)
|
||||
.append("\");\n")
|
||||
.append("\treturn _result");
|
||||
}
|
||||
.append(", \"")
|
||||
.append(entity)
|
||||
.append("\", ")
|
||||
.append(paramName)
|
||||
.append(')');
|
||||
}
|
||||
declaration
|
||||
.append(";\n");
|
||||
|
@ -139,7 +115,9 @@ public class IdFinderMethod extends AbstractFinderMethod {
|
|||
}
|
||||
else if (!nullable) {
|
||||
declaration
|
||||
.append("\tvar _result = ");
|
||||
.append("\treturn ")
|
||||
.append(annotationMetaEntity.staticImport("org.hibernate.exception.spi.Exceptions", "require"))
|
||||
.append('(');
|
||||
}
|
||||
else {
|
||||
declaration
|
||||
|
@ -149,6 +127,31 @@ public class IdFinderMethod extends AbstractFinderMethod {
|
|||
.append(sessionName);
|
||||
}
|
||||
|
||||
@Override
|
||||
void convertExceptions(StringBuilder declaration) {
|
||||
if (dataRepository) {
|
||||
if ( !nullable && containerType==null ) {
|
||||
declaration
|
||||
.append("\t}\n")
|
||||
.append("\tcatch (")
|
||||
.append(annotationMetaEntity.importType("org.hibernate.ObjectNotFoundException"))
|
||||
.append(" exception) {\n")
|
||||
.append("\t\tthrow new ")
|
||||
.append(annotationMetaEntity.importType("jakarta.data.exceptions.EmptyResultException"))
|
||||
.append("(exception.getMessage(), exception);\n");
|
||||
}
|
||||
declaration
|
||||
.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.getMessage(), exception);\n")
|
||||
.append("\t}\n");
|
||||
}
|
||||
}
|
||||
|
||||
private void findWithFetchProfiles(StringBuilder declaration) {
|
||||
unwrapSession( declaration );
|
||||
declaration
|
||||
|
@ -182,13 +185,4 @@ public class IdFinderMethod extends AbstractFinderMethod {
|
|||
declaration
|
||||
.append(")");
|
||||
}
|
||||
|
||||
private static void nullCheck(StringBuilder declaration, String parameterName) {
|
||||
declaration
|
||||
.append("\tif (")
|
||||
.append(parameterName)
|
||||
.append(" == null) throw new IllegalArgumentException(\"Null ")
|
||||
.append(parameterName)
|
||||
.append("\");\n");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ public class LifecycleMethod extends AbstractAnnotatedMethod {
|
|||
public String getAttributeDeclarationString() {
|
||||
StringBuilder declaration = new StringBuilder();
|
||||
preamble(declaration);
|
||||
nullCheck(declaration);
|
||||
nullCheck(declaration, parameterName);
|
||||
declaration.append("\ttry {\n");
|
||||
delegateCall(declaration);
|
||||
returnArgument(declaration);
|
||||
|
@ -196,15 +196,6 @@ public class LifecycleMethod extends AbstractAnnotatedMethod {
|
|||
}
|
||||
}
|
||||
|
||||
private void nullCheck(StringBuilder declaration) {
|
||||
declaration
|
||||
.append("\tif (")
|
||||
.append(parameterName)
|
||||
.append(" == null) throw new IllegalArgumentException(\"Null ")
|
||||
.append(parameterName)
|
||||
.append("\");\n");
|
||||
}
|
||||
|
||||
private void convertException(StringBuilder declaration, String exception, String convertedException) {
|
||||
declaration
|
||||
.append("\tcatch (")
|
||||
|
|
Loading…
Reference in New Issue