jeejz commented on code in PR #2298:
URL:
https://github.com/apache/incubator-kie-kogito-apps/pull/2298#discussion_r2822583669
##########
data-audit/kogito-addons-data-audit-jpa/kogito-addons-data-audit-jpa-common/src/main/java/org/kie/kogito/app/audit/jpa/queries/mapper/PojoMapper.java:
##########
@@ -46,14 +51,60 @@ public PojoMapper(Class<T> clazz) {
@Override
public List<T> produce(List<Object[]> data) {
List<T> transformed = new ArrayList<>();
+ Class<?>[] paramTypes = defaultConstructor.getParameterTypes();
for (Object[] row : data) {
try {
- transformed.add(defaultConstructor.newInstance(row));
+ // Hibernate 7 changed default return types for native queries
+ // (e.g. OffsetDateTime instead of java.util.Date, Long
instead of Integer).
+ // Convert each value to match the constructor's declared
parameter types.
+ Object[] convertedRow = convertTypes(row, paramTypes);
+ transformed.add(defaultConstructor.newInstance(convertedRow));
} catch (InstantiationException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException e) {
LOGGER.error("Could not transform data", e);
}
}
return transformed;
}
+ private static Object[] convertTypes(Object[] row, Class<?>[] paramTypes) {
+ Object[] result = new Object[row.length];
+ for (int i = 0; i < row.length; i++) {
+ result[i] = (i < paramTypes.length) ? convertValue(row[i],
paramTypes[i]) : row[i];
+ }
+ return result;
+ }
+
+ private static Object convertValue(Object value, Class<?> targetType) {
+ if (value == null || targetType.isInstance(value)) {
+ return value;
+ }
+ // Hibernate 7 returns java.time types instead of java.util.Date
+ if (targetType == Date.class) {
+ if (value instanceof OffsetDateTime) {
+ return Date.from(((OffsetDateTime) value).toInstant());
+ }
+ if (value instanceof Instant) {
+ return Date.from((Instant) value);
+ }
+ if (value instanceof LocalDateTime) {
+ return Date.from(((LocalDateTime)
value).atZone(ZoneId.of("UTC")).toInstant());
+ }
+ }
+ // Hibernate 7 may return different numeric types for native query
columns
+ if (targetType == Integer.class || targetType == int.class) {
+ if (value instanceof Number) {
+ return ((Number) value).intValue();
+ }
+ }
+ if (targetType == Long.class || targetType == long.class) {
+ if (value instanceof Number) {
+ return ((Number) value).longValue();
+ }
+ }
+ if (targetType == String.class) {
+ return value.toString();
Review Comment:
removed this check.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]