diff --git a/dev/com.ibm.ws.jpa.tests.jpa_32_fat/test-applications/jakartadata/src/io/openliberty/jpa/data/tests/models/DemographicInfoWithOffset.java b/dev/com.ibm.ws.jpa.tests.jpa_32_fat/test-applications/jakartadata/src/io/openliberty/jpa/data/tests/models/DemographicInfoWithOffset.java new file mode 100644 index 00000000000..f981853d3cc --- /dev/null +++ b/dev/com.ibm.ws.jpa.tests.jpa_32_fat/test-applications/jakartadata/src/io/openliberty/jpa/data/tests/models/DemographicInfoWithOffset.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * Copyright (c) 2024 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ +package io.openliberty.jpa.data.tests.models; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; + +/** + * Recreate from io.openliberty.data.internal_fat_jpa + * Same as DemographicInfo but uses OffsetDateTime instead of Instance + */ +@Entity +public class DemographicInfoWithOffset { + + @Column + public OffsetDateTime collectedOn; + + @GeneratedValue + @Id + public BigInteger id; + + @Column + public BigDecimal publicDebt; + + @Column + public BigDecimal intragovernmentalDebt; + + @Column + public BigInteger numFullTimeWorkers; + + public static DemographicInfoWithOffset of(int year, int month, int day, + long numFullTimeWorkers, + double intragovernmentalDebt, double publicDebt) { + DemographicInfoWithOffset inst = new DemographicInfoWithOffset(); + inst.collectedOn = ZonedDateTime.of(year, month, day, 12, 0, 0, 0, ZoneId.of("America/New_York")).toOffsetDateTime(); + inst.numFullTimeWorkers = BigInteger.valueOf(numFullTimeWorkers); + inst.intragovernmentalDebt = BigDecimal.valueOf(intragovernmentalDebt); + inst.publicDebt = BigDecimal.valueOf(publicDebt); + return inst; + } + + @Override + public String toString() { + return "DemographicInfo from " + collectedOn; + } +} \ No newline at end of file diff --git a/dev/com.ibm.ws.jpa.tests.jpa_32_fat/test-applications/jakartadata/src/io/openliberty/jpa/data/tests/web/JakartaDataRecreateServlet.java b/dev/com.ibm.ws.jpa.tests.jpa_32_fat/test-applications/jakartadata/src/io/openliberty/jpa/data/tests/web/JakartaDataRecreateServlet.java index 68799131ee3..c7b1491724b 100644 --- a/dev/com.ibm.ws.jpa.tests.jpa_32_fat/test-applications/jakartadata/src/io/openliberty/jpa/data/tests/web/JakartaDataRecreateServlet.java +++ b/dev/com.ibm.ws.jpa.tests.jpa_32_fat/test-applications/jakartadata/src/io/openliberty/jpa/data/tests/web/JakartaDataRecreateServlet.java @@ -30,6 +30,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; @@ -53,6 +54,7 @@ import io.openliberty.jpa.data.tests.models.CityId; import io.openliberty.jpa.data.tests.models.Coordinate; import io.openliberty.jpa.data.tests.models.DemographicInfo; +import io.openliberty.jpa.data.tests.models.DemographicInfoWithOffset; import io.openliberty.jpa.data.tests.models.DemographicInformation; import io.openliberty.jpa.data.tests.models.Item; import io.openliberty.jpa.data.tests.models.Line; @@ -1354,10 +1356,10 @@ public void testOLGH28898() throws Exception { public void testOLGH29781() throws Exception { ZoneId ET = ZoneId.of("America/New_York"); Instant when = ZonedDateTime.of(2022, 4, 29, 12, 0, 0, 0, ET) - .toInstant(); + .toInstant(); Store s1 = Store.of(2022, 4, 29, "Billy", 12L); Store s2 = Store.of(2024, 5, 12, "Bobby", 9L); - + int count; tx.begin(); @@ -1368,8 +1370,8 @@ public void testOLGH29781() throws Exception { tx.begin(); try { count = em.createQuery("DELETE FROM Store WHERE this.time>:when") - .setParameter("when", when) - .executeUpdate(); + .setParameter("when", when) + .executeUpdate(); tx.commit(); } catch (Exception e) { tx.rollback(); @@ -1568,6 +1570,55 @@ public void testOLGH29443ZonedDateTime() throws Exception { } } + @Test //Original issue: https://github.com/OpenLiberty/open-liberty/issues/29443 + public void testOLGH29443OffsetDateTime() throws Exception { + deleteAllEntities(DemographicInfoWithOffset.class); + + ZoneId ET = ZoneId.of("America/New_York"); + OffsetDateTime when = ZonedDateTime.of(2022, 4, 29, 12, 0, 0, 0, ET).toOffsetDateTime(); + + DemographicInfoWithOffset US2022 = DemographicInfoWithOffset.of(2022, 4, 29, 132250000, 6526909395140.41, 23847245116757.60); + DemographicInfoWithOffset US2007 = DemographicInfoWithOffset.of(2007, 4, 30, 121090000, 3833110332444.19, 5007058051986.64); + + List results; + + tx.begin(); + em.persist(US2022); + em.persist(US2007); + tx.commit(); + + List errors = new ArrayList<>(); + + Thread.sleep(Duration.ofSeconds(1).toMillis()); + + for (int i = 0; i < 10; i++) { + System.out.println("Executing SELECT query, iteration: " + i); + + tx.begin(); + results = em + .createQuery("SELECT this.numFullTimeWorkers FROM DemographicInfoWithOffset WHERE this.collectedOn=:when", + BigInteger.class) + .setParameter("when", when) + .getResultList(); + tx.commit(); + + try { + assertNotNull("Query should not have returned null after iteration " + i, results); + // Recreate - an empty list is returned + assertFalse("Query should not have returned an empty list after iteration " + i, results.isEmpty()); + assertEquals("Query should not have returned more than one result after iteration " + i, 1, + results.size()); + assertEquals(US2022.numFullTimeWorkers, results.get(0)); + } catch (AssertionError e) { + errors.add(e); + } + } + + if (!errors.isEmpty()) { + throw new AssertionError("Executing the same query returned incorrect results " + errors.size() + " out of 10 executions", errors.get(0)); + } + } + @Test @Ignore("Reference issue: https://github.com/OpenLiberty/open-liberty/issues/29475") public void testOLGH29475() throws Exception {