-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jakarta Persistence 3.2 new feature - java.lang.Record support in @Em…
…bedded parts Signed-off-by: Radek Felcman <[email protected]>
- Loading branch information
Showing
20 changed files
with
1,089 additions
and
153 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...rc/test/java/org/eclipse/persistence/testing/tests/junit/policies/NestedDetailRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
*/ | ||
|
||
// Contributors: | ||
// Oracle - initial API and implementation | ||
package org.eclipse.persistence.testing.tests.junit.policies; | ||
|
||
import java.util.Date; | ||
import java.util.Objects; | ||
|
||
public record NestedDetailRecord(float floatAttribute, Date dateAttribute) { | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
NestedDetailRecord that = (NestedDetailRecord) o; | ||
return Float.compare(floatAttribute, that.floatAttribute) == 0 && Objects.equals(dateAttribute, that.dateAttribute); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(floatAttribute, dateAttribute); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...rc/test/java/org/eclipse/persistence/testing/tests/junit/policies/NestedMasterRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
*/ | ||
|
||
// Contributors: | ||
// Oracle - initial API and implementation | ||
package org.eclipse.persistence.testing.tests.junit.policies; | ||
|
||
import java.util.Objects; | ||
|
||
public record NestedMasterRecord(int intAttribute, String stringAttribute, NestedDetailRecord nestedDetailRecord) { | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
NestedMasterRecord that = (NestedMasterRecord) o; | ||
return intAttribute == that.intAttribute && Objects.equals(stringAttribute, that.stringAttribute) && Objects.equals(nestedDetailRecord, that.nestedDetailRecord); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(intAttribute, stringAttribute, nestedDetailRecord); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
.../test/java/org/eclipse/persistence/testing/tests/junit/policies/RecordCopyPolicyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
*/ | ||
|
||
// Contributors: | ||
// Oracle - initial API and implementation from Oracle TopLink | ||
package org.eclipse.persistence.testing.tests.junit.policies; | ||
|
||
import org.eclipse.persistence.descriptors.copying.RecordCopyPolicy; | ||
import org.junit.Test; | ||
|
||
import java.time.Instant; | ||
import java.util.Date; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class RecordCopyPolicyTest { | ||
|
||
private final int INT_ATTR = 1; | ||
private final String STRING_ATTR = "abcde"; | ||
private final float FLOAT_ATTR = 1.1F; | ||
private final Date DATE_ATTR = Date.from(Instant.parse("2024-05-27T10:15:30.00Z")); | ||
|
||
@Test | ||
public void cloneNestedRecordTest() { | ||
NestedDetailRecord nestedDetailRecord = new NestedDetailRecord(FLOAT_ATTR, DATE_ATTR); | ||
NestedMasterRecord source = new NestedMasterRecord(INT_ATTR, STRING_ATTR, nestedDetailRecord); | ||
RecordCopyPolicy recordCopyPolicy = new RecordCopyPolicy(); | ||
NestedMasterRecord clone = (NestedMasterRecord)recordCopyPolicy.buildClone(source, null); | ||
assertEquals(source, clone); | ||
} | ||
|
||
@Test | ||
public void cloneNestedRecordWithNullTest() { | ||
NestedDetailRecord nestedDetailRecord = new NestedDetailRecord(FLOAT_ATTR, DATE_ATTR); | ||
NestedMasterRecord source = new NestedMasterRecord(INT_ATTR, null, nestedDetailRecord); | ||
RecordCopyPolicy recordCopyPolicy = new RecordCopyPolicy(); | ||
NestedMasterRecord clone = (NestedMasterRecord)recordCopyPolicy.buildClone(source, null); | ||
assertEquals(source, clone); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...a/org/eclipse/persistence/testing/tests/junit/policies/RecordInstantiationPolicyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
*/ | ||
|
||
// Contributors: | ||
// Oracle - initial API and implementation from Oracle TopLink | ||
package org.eclipse.persistence.testing.tests.junit.policies; | ||
|
||
import org.eclipse.persistence.internal.descriptors.RecordInstantiationPolicy; | ||
import org.junit.Test; | ||
|
||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
public class RecordInstantiationPolicyTest { | ||
|
||
private final int INT_ATTR = 1; | ||
private final String STRING_ATTR = "abcde"; | ||
private final float FLOAT_ATTR = 1.1F; | ||
private final Date DATE_ATTR = Date.from(Instant.parse("2024-05-27T10:15:30.00Z")); | ||
|
||
@Test | ||
public void newNestedRecordTest() { | ||
Class clazz = NestedMasterRecord.class; | ||
//Values order is important. It must be same as order of Record attributes. | ||
List values = new ArrayList<>(); | ||
values.add(INT_ATTR); | ||
values.add(STRING_ATTR); | ||
values.add(FLOAT_ATTR); | ||
values.add(DATE_ATTR); | ||
RecordInstantiationPolicy recordInstantiationPolicy = new RecordInstantiationPolicy(); | ||
NestedMasterRecord nestedMasterRecord = (NestedMasterRecord) recordInstantiationPolicy.newRecord(clazz, values); | ||
assertEquals(INT_ATTR, nestedMasterRecord.intAttribute()); | ||
assertEquals(STRING_ATTR, nestedMasterRecord.stringAttribute()); | ||
assertEquals(FLOAT_ATTR, nestedMasterRecord.nestedDetailRecord().floatAttribute(), 0f); | ||
assertEquals(DATE_ATTR, nestedMasterRecord.nestedDetailRecord().dateAttribute()); | ||
} | ||
|
||
@Test | ||
public void newNestedRecordWithNullTest() { | ||
Class clazz = NestedMasterRecord.class; | ||
//Values order is important. It must be same as order of Record attributes. | ||
List values = new ArrayList<>(); | ||
values.add(INT_ATTR); | ||
values.add(null); | ||
values.add(FLOAT_ATTR); | ||
values.add(DATE_ATTR); | ||
RecordInstantiationPolicy recordInstantiationPolicy = new RecordInstantiationPolicy(); | ||
NestedMasterRecord nestedMasterRecord = (NestedMasterRecord) recordInstantiationPolicy.newRecord(clazz, values); | ||
assertEquals(INT_ATTR, nestedMasterRecord.intAttribute()); | ||
assertNull(nestedMasterRecord.stringAttribute()); | ||
assertEquals(FLOAT_ATTR, nestedMasterRecord.nestedDetailRecord().floatAttribute(), 0f); | ||
assertEquals(DATE_ATTR, nestedMasterRecord.nestedDetailRecord().dateAttribute()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...ence.core/src/main/java/org/eclipse/persistence/descriptors/copying/RecordCopyPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
*/ | ||
|
||
// Contributors: | ||
// Oracle - initial API and implementation from Oracle TopLink | ||
package org.eclipse.persistence.descriptors.copying; | ||
|
||
import org.eclipse.persistence.exceptions.DescriptorException; | ||
import org.eclipse.persistence.sessions.Session; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.RecordComponent; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* <p><b>Purpose</b>: This is the copy policy for {@code java.lang.Record}. | ||
* <p> | ||
* It creates a copy by creating a new instance. As {@code java.lang.Record} if immutable | ||
* all cloned attributes must be collected and passed to the constructor. | ||
*/ | ||
public class RecordCopyPolicy extends AbstractCopyPolicy { | ||
public RecordCopyPolicy() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public Object buildClone(Object domainObject, Session session) throws DescriptorException { | ||
return cloneRecord((Record) domainObject); | ||
} | ||
|
||
@Override | ||
public boolean buildsNewInstance() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + "()"; | ||
} | ||
|
||
//There is limited functionality some complex nested objects shouldn't be cloned. | ||
private <R extends Record> R cloneRecord(R template) { | ||
try { | ||
ArrayList<Class<?>> types = new ArrayList<>(); | ||
ArrayList values = new ArrayList<>(); | ||
for (RecordComponent component : template.getClass().getRecordComponents()) { | ||
types.add(component.getType()); | ||
Object value = component.getAccessor().invoke(template); | ||
if (value instanceof Record) { | ||
value = cloneRecord((Record) value); | ||
} | ||
values.add(value); | ||
} | ||
Constructor<? extends Record> canonical = template.getClass().getDeclaredConstructor(types.toArray(Class[]::new)); | ||
var result = (R) canonical.newInstance(values.toArray(Object[]::new)); | ||
return result; | ||
} catch (ReflectiveOperationException e) { | ||
throw new RuntimeException("Record clone failed: " + e, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.