From a4f20f7350711191f69cebacc60c875110c377f9 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 14 Nov 2024 09:40:39 -0800 Subject: [PATCH] Manually narrow if primary key field is int. Yay for unit tests that caught this runtime failure! --- .../com/samskivert/depot/impl/DepotMarshaller.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/samskivert/depot/impl/DepotMarshaller.java b/src/main/java/com/samskivert/depot/impl/DepotMarshaller.java index 8285bf4..723796a 100644 --- a/src/main/java/com/samskivert/depot/impl/DepotMarshaller.java +++ b/src/main/java/com/samskivert/depot/impl/DepotMarshaller.java @@ -660,7 +660,16 @@ public class DepotMarshaller implements QueryMarshal try { long nextValue = vg.nextGeneratedValue(conn, liaison, stmt); - field.set(po, nextValue); + // We get the next generated value from the database as a long, but we have to + // narrow it if the primary key field is an `int` or `Integer`. We used to use int + // as the default auto-generated key type, so we need this for backwards compat. + if (field.getType().equals(int.class) || field.getType().equals(Integer.class)) { + if (nextValue > Integer.MAX_VALUE) throw new IllegalStateException( + "Primary key too large to fit in 'int': " + nextValue); + field.set(po, (int)nextValue); + } else { + field.set(po, nextValue); // fingers crossed! + } } catch (Exception e) { throw new IllegalStateException(