HHH-18216 only do it for generated ids

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-06-02 13:01:26 +02:00
parent 1a5456dde1
commit e895283c53
4 changed files with 27 additions and 11 deletions

View File

@ -1375,13 +1375,26 @@ public class AnnotationMetaEntity extends AnnotationMeta {
operation,
context.addNonnullAnnotation(),
isIterableLifecycleParameter(parameterType),
returnArgument
returnArgument,
hasGeneratedId(declaredType)
)
);
}
}
}
private boolean hasGeneratedId(DeclaredType entityType) {
final TypeElement typeElement = (TypeElement) entityType.asElement();
for ( Element member : context.getAllMembers(typeElement) ) {
if ( hasAnnotation(member, GENERATED_VALUE)
&& hasAnnotation(member, ID) ) {
return true;
}
//TODO: look for generator annotations
}
return false;
}
private static boolean isIterableLifecycleParameter(TypeMirror parameterType) {
switch (parameterType.getKind()) {
case ARRAY:

View File

@ -18,6 +18,7 @@ public class LifecycleMethod extends AbstractAnnotatedMethod {
private final boolean addNonnullAnnotation;
private final boolean iterateParameter;
private final boolean returnArgument;
private final boolean hasGeneratedId;
public LifecycleMethod(
AnnotationMetaEntity annotationMetaEntity,
@ -30,7 +31,8 @@ public class LifecycleMethod extends AbstractAnnotatedMethod {
String operationName,
boolean addNonnullAnnotation,
boolean iterateParameter,
boolean returnArgument) {
boolean returnArgument,
boolean hasGeneratedId) {
super(annotationMetaEntity, method, sessionName, sessionType);
this.entity = entity;
this.methodName = methodName;
@ -39,6 +41,7 @@ public class LifecycleMethod extends AbstractAnnotatedMethod {
this.addNonnullAnnotation = addNonnullAnnotation;
this.iterateParameter = iterateParameter;
this.returnArgument = returnArgument;
this.hasGeneratedId = hasGeneratedId;
}
@Override
@ -131,8 +134,7 @@ public class LifecycleMethod extends AbstractAnnotatedMethod {
.append(parameterName)
.append(") {\n\t");
}
if ( "upsert".equals(operationName) ) {
//TODO: only do this for @GeneratedValue
if ( "upsert".equals(operationName) && hasGeneratedId ) {
declaration
.append("\t\tif (")
.append(sessionName)

View File

@ -38,7 +38,7 @@ public final class Constants {
public static final String ELEMENT_COLLECTION = "jakarta.persistence.ElementCollection";
public static final String ACCESS = "jakarta.persistence.Access";
public static final String CONVERT = "jakarta.persistence.Convert";
public static final String HIBERNATE_TYPE = "org.hibernate.annotations.Type";
public static final String GENERATED_VALUE = "jakarta.persistence.GeneratedValue";
public static final String NAMED_QUERY = "jakarta.persistence.NamedQuery";
public static final String NAMED_QUERIES = "jakarta.persistence.NamedQueries";

View File

@ -457,12 +457,13 @@ public final class TypeUtils {
}
private static @Nullable AccessType getAccessTypeOfIdAnnotation(Element element) {
final ElementKind kind = element.getKind();
if ( kind == ElementKind.FIELD || kind == ElementKind.METHOD ) {
return kind == ElementKind.FIELD ? AccessType.FIELD : AccessType.PROPERTY;
}
else {
return null;
switch ( element.getKind() ) {
case FIELD:
return AccessType.FIELD;
case METHOD:
return AccessType.PROPERTY;
default:
return null;
}
}