|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, |
| 7 | + * or the Eclipse Distribution License v. 1.0 which is available at |
| 8 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 11 | + */ |
| 12 | + |
| 13 | +package org.eclipse.persistence.jpa.test.record; |
| 14 | + |
| 15 | +import jakarta.persistence.EntityManager; |
| 16 | +import jakarta.persistence.EntityManagerFactory; |
| 17 | + |
| 18 | +import org.eclipse.persistence.jpa.test.framework.DDLGen; |
| 19 | +import org.eclipse.persistence.jpa.test.framework.Emf; |
| 20 | +import org.eclipse.persistence.jpa.test.framework.EmfRunner; |
| 21 | +import org.eclipse.persistence.jpa.test.record.model.RecordEntity; |
| 22 | +import org.eclipse.persistence.jpa.test.record.model.RecordId; |
| 23 | +import org.eclipse.persistence.jpa.test.record.model.RecordValue; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | + |
| 27 | +import java.util.UUID; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertEquals; |
| 30 | +import static org.junit.Assert.assertNotNull; |
| 31 | +import static org.junit.Assert.assertTrue; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@code java.lang.Record} used as JPA {@code @Embeddable} types. |
| 35 | + * <p> |
| 36 | + * This test covers: |
| 37 | + * <ul> |
| 38 | + * <li>{@code @EmbeddedId} with a record type</li> |
| 39 | + * <li>{@code @Embedded} attribute with a record type</li> |
| 40 | + * <li>Persist, find, and merge operations</li> |
| 41 | + * </ul> |
| 42 | + * <p> |
| 43 | + * Prior to the fix for <a href="https://github.com/eclipse-ee4j/eclipselink/issues/2656">#2656</a>, |
| 44 | + * EclipseLink threw {@code IllegalAccessException: Can not set final field} when |
| 45 | + * method-accessed embeddable records were processed. The root cause was that |
| 46 | + * {@code RecordCopyPolicy} and {@code RecordInstantiationPolicy} were only |
| 47 | + * initialized for field-accessed descriptors, not method-accessed ones. |
| 48 | + */ |
| 49 | +@RunWith(EmfRunner.class) |
| 50 | +public class TestRecordEmbeddable { |
| 51 | + |
| 52 | + @Emf(createTables = DDLGen.DROP_CREATE, classes = { |
| 53 | + RecordEntity.class, RecordId.class, RecordValue.class }) |
| 54 | + private EntityManagerFactory emf; |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testPersistAndFind() { |
| 58 | + EntityManager em = emf.createEntityManager(); |
| 59 | + try { |
| 60 | + em.getTransaction().begin(); |
| 61 | + |
| 62 | + UUID uuid = UUID.randomUUID(); |
| 63 | + RecordId id = new RecordId(uuid); |
| 64 | + RecordValue value = new RecordValue("test-description", 42); |
| 65 | + RecordEntity entity = new RecordEntity(id, "test-entity", value); |
| 66 | + |
| 67 | + em.persist(entity); |
| 68 | + em.getTransaction().commit(); |
| 69 | + |
| 70 | + // Clear to force a fresh read from the database |
| 71 | + em.clear(); |
| 72 | + |
| 73 | + RecordEntity found = em.find(RecordEntity.class, id); |
| 74 | + assertNotNull("Entity should be found after persist", found); |
| 75 | + assertEquals("test-entity", found.getName()); |
| 76 | + assertNotNull("Embedded value should not be null", found.getValue()); |
| 77 | + assertEquals("test-description", found.getValue().description()); |
| 78 | + assertEquals(42, found.getValue().amount()); |
| 79 | + } finally { |
| 80 | + if (em.getTransaction().isActive()) { |
| 81 | + em.getTransaction().rollback(); |
| 82 | + } |
| 83 | + em.close(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testMerge() { |
| 89 | + EntityManager em = emf.createEntityManager(); |
| 90 | + try { |
| 91 | + // Persist |
| 92 | + em.getTransaction().begin(); |
| 93 | + UUID uuid = UUID.randomUUID(); |
| 94 | + RecordId id = new RecordId(uuid); |
| 95 | + RecordValue value = new RecordValue("original", 10); |
| 96 | + RecordEntity entity = new RecordEntity(id, "original-name", value); |
| 97 | + em.persist(entity); |
| 98 | + em.getTransaction().commit(); |
| 99 | + em.clear(); |
| 100 | + |
| 101 | + // Merge with updated values |
| 102 | + em.getTransaction().begin(); |
| 103 | + RecordValue updatedValue = new RecordValue("updated", 20); |
| 104 | + RecordEntity updated = new RecordEntity(id, "updated-name", updatedValue); |
| 105 | + RecordEntity merged = em.merge(updated); |
| 106 | + em.getTransaction().commit(); |
| 107 | + |
| 108 | + assertNotNull("Merged entity should not be null", merged); |
| 109 | + assertEquals("updated-name", merged.getName()); |
| 110 | + assertEquals("updated", merged.getValue().description()); |
| 111 | + assertEquals(20, merged.getValue().amount()); |
| 112 | + } finally { |
| 113 | + if (em.getTransaction().isActive()) { |
| 114 | + em.getTransaction().rollback(); |
| 115 | + } |
| 116 | + em.close(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testGetIdentifier() { |
| 122 | + UUID uuid = UUID.randomUUID(); |
| 123 | + RecordId id = new RecordId(uuid); |
| 124 | + RecordEntity entity = new RecordEntity(id, "test-entity", new RecordValue("desc", 1)); |
| 125 | + |
| 126 | + Object identifier = emf.getPersistenceUnitUtil().getIdentifier(entity); |
| 127 | + |
| 128 | + assertNotNull("Identifier should not be null", identifier); |
| 129 | + assertTrue("Identifier should be a RecordId", identifier instanceof RecordId); |
| 130 | + assertEquals("Identifier should equal the entity's embedded id", id, identifier); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testPersistAndFindWithNoArgConstructor() { |
| 135 | + EntityManager em = emf.createEntityManager(); |
| 136 | + try { |
| 137 | + em.getTransaction().begin(); |
| 138 | + |
| 139 | + // Mirror the original #2656 reproduction: the @EmbeddedId record |
| 140 | + // is created through its custom no-arg constructor. |
| 141 | + RecordId id = new RecordId(); |
| 142 | + RecordValue value = new RecordValue("no-arg-description", 7); |
| 143 | + RecordEntity entity = new RecordEntity(id, "no-arg-entity", value); |
| 144 | + |
| 145 | + em.persist(entity); |
| 146 | + em.getTransaction().commit(); |
| 147 | + em.clear(); |
| 148 | + |
| 149 | + RecordEntity found = em.find(RecordEntity.class, id); |
| 150 | + assertNotNull("Entity should be found after persist", found); |
| 151 | + assertEquals("no-arg-entity", found.getName()); |
| 152 | + assertEquals("no-arg-description", found.getValue().description()); |
| 153 | + } finally { |
| 154 | + if (em.getTransaction().isActive()) { |
| 155 | + em.getTransaction().rollback(); |
| 156 | + } |
| 157 | + em.close(); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments