Fix MapAttributeConverter to return null on null attribute (2) (#2045)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-11-15 21:58:05 +02:00
committed by GitHub
parent fef29ddddb
commit 3ecaa24b10
5 changed files with 13 additions and 7 deletions

View File

@@ -103,7 +103,7 @@ public class JpaAction extends AbstractJpaTenantAwareBaseEntity implements Actio
ActionType.SOFT, 1,
ActionType.TIMEFORCED, 2,
ActionType.DOWNLOAD_ONLY, 3
));
), null);
}
}
@Column(name = "action_type", nullable = false)
@@ -136,7 +136,7 @@ public class JpaAction extends AbstractJpaTenantAwareBaseEntity implements Actio
put(Status.CANCEL_REJECTED, 9);
put(Status.DOWNLOADED, 10);
put(Status.WAIT_FOR_CONFIRMATION, 11);
}});
}}, null);
}
}
@Column(name = "status", nullable = false)

View File

@@ -92,7 +92,7 @@ public class JpaRollout extends AbstractJpaNamedEntity implements Rollout, Event
put(RolloutStatus.WAITING_FOR_APPROVAL, 11);
put(RolloutStatus.APPROVAL_DENIED, 12);
put(RolloutStatus.STOPPING, 13);
}});
}}, null);
}
}
@Column(name = "status", nullable = false)

View File

@@ -69,7 +69,7 @@ public class JpaRolloutGroup extends AbstractJpaNamedEntity implements RolloutGr
RolloutGroupStatus.ERROR, 3,
RolloutGroupStatus.RUNNING, 4,
RolloutGroupStatus.CREATING, 5
));
), null);
}
}

View File

@@ -129,7 +129,7 @@ public class JpaTarget extends AbstractJpaNamedEntity implements Target, EventAw
TargetUpdateStatus.PENDING, 2,
TargetUpdateStatus.ERROR, 3,
TargetUpdateStatus.REGISTERED, 4
));
), null);
}
}
@Column(name = "update_status", nullable = false)

View File

@@ -10,6 +10,7 @@
package org.eclipse.hawkbit.repository.jpa.utils;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import jakarta.persistence.AttributeConverter;
@@ -18,9 +19,11 @@ public class MapAttributeConverter<JAVA_TYPE extends Enum<JAVA_TYPE>, DB_TYPE> i
private final Map<JAVA_TYPE, DB_TYPE> javaToDbMap;
private final Map<DB_TYPE, JAVA_TYPE> dbToJavaMap;
private final DB_TYPE nullMapping;
protected MapAttributeConverter(final Map<JAVA_TYPE, DB_TYPE> javaToDbMap) {
protected MapAttributeConverter(final Map<JAVA_TYPE, DB_TYPE> javaToDbMap, final DB_TYPE nullMapping) {
this.javaToDbMap = javaToDbMap;
this.nullMapping = nullMapping;
this.dbToJavaMap = javaToDbMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
if (javaToDbMap.size() != dbToJavaMap.size()) {
throw new IllegalArgumentException("Duplicate values in javaToDbMap");
@@ -30,13 +33,16 @@ public class MapAttributeConverter<JAVA_TYPE extends Enum<JAVA_TYPE>, DB_TYPE> i
@Override
public DB_TYPE convertToDatabaseColumn(final JAVA_TYPE attribute) {
if (attribute == null) {
return null;
return nullMapping;
}
return javaToDbMap.get(attribute);
}
@Override
public JAVA_TYPE convertToEntityAttribute(final DB_TYPE dbData) {
if (Objects.equals(dbData, nullMapping)) {
return null;
}
return dbToJavaMap.get(dbData);
}
}