|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.envers.test.integration.data; |
| 6 | + |
| 7 | + |
| 8 | +import jakarta.persistence.Embedded; |
| 9 | +import jakarta.persistence.Entity; |
| 10 | +import jakarta.persistence.EntityManager; |
| 11 | +import jakarta.persistence.Id; |
| 12 | +import org.hibernate.envers.AuditReader; |
| 13 | +import org.hibernate.envers.Audited; |
| 14 | +import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase; |
| 15 | +import org.hibernate.orm.test.envers.Priority; |
| 16 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | + |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests that {@link jakarta.persistence.Embedded} works correctly when combined with Java record classes |
| 24 | + * in the context of Envers auditing. |
| 25 | + * |
| 26 | + * @author Minjae Seon |
| 27 | + */ |
| 28 | +@JiraKey(value = "HHH-18691") |
| 29 | +public class RecordFieldEntityTest extends BaseEnversJPAFunctionalTestCase { |
| 30 | + record TestRecord(String foo, String bar) {} |
| 31 | + |
| 32 | + static class TestEmbeddedClass { |
| 33 | + public TestEmbeddedClass() {} |
| 34 | + public TestEmbeddedClass(String foo, String bar) { |
| 35 | + this.foo = foo; |
| 36 | + this.bar = bar; |
| 37 | + } |
| 38 | + |
| 39 | + private String foo; |
| 40 | + private String bar; |
| 41 | + |
| 42 | + public String getFoo() { |
| 43 | + return foo; |
| 44 | + } |
| 45 | + |
| 46 | + public String getBar() { |
| 47 | + return bar; |
| 48 | + } |
| 49 | + |
| 50 | + public void setFoo(String foo) { |
| 51 | + this.foo = foo; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Entity |
| 56 | + @Audited |
| 57 | + static class WithRecord { |
| 58 | + @Id |
| 59 | + private Integer id; |
| 60 | + |
| 61 | + @Embedded |
| 62 | + private TestRecord testRecord; |
| 63 | + |
| 64 | + public Integer getId() { |
| 65 | + return id; |
| 66 | + } |
| 67 | + |
| 68 | + static WithRecord of(int id, String foo, String bar) { |
| 69 | + WithRecord withRecord = new WithRecord(); |
| 70 | + |
| 71 | + withRecord.id = id; |
| 72 | + withRecord.testRecord = new TestRecord(foo, bar); |
| 73 | + return withRecord; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Entity |
| 78 | + @Audited |
| 79 | + static class WithoutRecord { |
| 80 | + @Id |
| 81 | + private Integer id; |
| 82 | + |
| 83 | + @Embedded |
| 84 | + private TestEmbeddedClass testEmbeddedClass; |
| 85 | + |
| 86 | + public Integer getId() { |
| 87 | + return id; |
| 88 | + } |
| 89 | + |
| 90 | + public TestEmbeddedClass getTestEmbeddedClass() { |
| 91 | + return testEmbeddedClass; |
| 92 | + } |
| 93 | + |
| 94 | + static WithoutRecord of(int id, String foo, String bar) { |
| 95 | + WithoutRecord withoutRecord = new WithoutRecord(); |
| 96 | + |
| 97 | + withoutRecord.id = id; |
| 98 | + withoutRecord.testEmbeddedClass = new TestEmbeddedClass(foo, bar); |
| 99 | + |
| 100 | + return withoutRecord; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected Class<?>[] getAnnotatedClasses() { |
| 106 | + return new Class<?>[]{ WithRecord.class, WithoutRecord.class }; |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @Priority(10) |
| 111 | + public void initData() { |
| 112 | + EntityManager em = getEntityManager(); |
| 113 | + |
| 114 | + em.getTransaction().begin(); |
| 115 | + |
| 116 | + // Create WithRecord Entity |
| 117 | + WithRecord withRecord = WithRecord.of( 1, "foo", "bar" ); |
| 118 | + em.persist( withRecord ); |
| 119 | + |
| 120 | + // Create WithoutRecord |
| 121 | + WithoutRecord withoutRecord = WithoutRecord.of( 1, "foo", "bar" ); |
| 122 | + em.persist( withoutRecord ); |
| 123 | + |
| 124 | + em.getTransaction().commit(); |
| 125 | + |
| 126 | + em.close(); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testLoadRecordData() { |
| 131 | + AuditReader auditReader = getAuditReader(); |
| 132 | + WithRecord recordRev = auditReader.find( WithRecord.class, 1, 1 ); |
| 133 | + |
| 134 | + assertEquals("WithRecord.TestRecord.foo equals foo", "foo", recordRev.testRecord.foo()); |
| 135 | + assertEquals("WithRecord.TestRecord.bar equals bar", "bar", recordRev.testRecord.bar()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testLoadWithoutRecordData() { |
| 140 | + AuditReader auditReader = getAuditReader(); |
| 141 | + WithoutRecord withoutRecordRev = auditReader.find( WithoutRecord.class, 1, 1 ); |
| 142 | + |
| 143 | + assertEquals("WithoutRecord.TestEmbeddedClass.foo equals foo", "foo", withoutRecordRev.getTestEmbeddedClass().getFoo()); |
| 144 | + assertEquals("WithoutRecord.TestEmbeddedClass.bar equals bar", "bar", withoutRecordRev.getTestEmbeddedClass().getBar()); |
| 145 | + } |
| 146 | +} |
0 commit comments