diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/mapKeyJoinColumn/Course.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/mapKeyJoinColumn/Course.java new file mode 100644 index 000000000..da675c790 --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/mapKeyJoinColumn/Course.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.annotations.mapKeyJoinColumn; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +@Entity +@Table(name = "SCHEMAGENMKJCCOURSE") +public class Course implements java.io.Serializable { + + private static final long serialVersionUID = 22L; + + @Id + @Column(name = "COURSEID") + private int id; + + @Column(name = "COURSENAME") + private String name; + + public Course() { + } + + public Course(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + return id == ((Course) o).id; + } + + @Override + public int hashCode() { + return id; + } + + @Override + public String toString() { + return getClass().getSimpleName() + "[id: " + id + "]"; + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/mapKeyJoinColumn/SeMapKeyJoinColumnSchemaGenerationAnnotationClient.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/mapKeyJoinColumn/SeMapKeyJoinColumnSchemaGenerationAnnotationClient.java new file mode 100644 index 000000000..9edf898c7 --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/mapKeyJoinColumn/SeMapKeyJoinColumnSchemaGenerationAnnotationClient.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.annotations.mapKeyJoinColumn; + +import ee.jakarta.tck.persistence.common.SchemaGenerationClientBase; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; + +import java.util.Map; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SeMapKeyJoinColumnSchemaGenerationAnnotationClient extends SchemaGenerationClientBase { + + private static final String TEST_CASE = "jpa_se_schemaGeneration_annotations_mapKeyJoinColumn"; + + @Override + public JavaArchive createDeployment() throws Exception { + String packageName = SeMapKeyJoinColumnSchemaGenerationAnnotationClient.class.getPackageName(); + String[] classes = { + packageName + ".Course", + packageName + ".Semester", + packageName + ".Student"}; + return createDeploymentJar(TEST_CASE + ".jar", packageName, classes); + } + + /** + * Verifies the {@code foreignKey} attribute of {@code @MapKeyJoinColumn} and + * {@code @MapKeyJoinColumns} is honored by schema generation for + * {@code @ManyToMany} relationships whose map key is an entity. The generated + * schema must contain the named foreign key constraints from the map key join + * column of each join table to the map key entity table, and the database + * must reject a join table row whose map key has no matching entity row. + */ + @Test + public void mapKeyJoinColumnForeignKeyTest() throws Throwable { + verifyGeneratedSchema(() -> { + var course1 = new Course(1, "course1"); + var course2 = new Course(2, "course2"); + var semester1 = new Semester(1, "semester1"); + var semester2 = new Semester(2, "semester2"); + var student = new Student(1); + student.setEnrollments(Map.of(course1, semester1, course2, semester2)); + student.setRetakes(Map.of(course1, semester2)); + persist(course1, course2, semester1, semester2, student); + + // After reloading, each course key must still map to the semester it was + // persisted with. Entities are compared by id because the provider may + // return proxies for the map keys and values. + var found = getEntityManager().find(Student.class, 1); + + var enrollments = semesterIdByCourseId(found.getEnrollments()); + assertEquals(2, enrollments.size()); + assertEquals(semester1.getId(), enrollments.get(course1.getId())); + assertEquals(semester2.getId(), enrollments.get(course2.getId())); + + var retakes = semesterIdByCourseId(found.getRetakes()); + assertEquals(1, retakes.size()); + assertEquals(semester2.getId(), retakes.get(course1.getId())); + + // The foreign key constraints must exist in the database metadata under the + // names given in the mapping, on the map key join column of each join table, + // referencing the primary key of the map key entity table. + assertForeignKey("SCHEMAGENMKJC_ENROLLMENTS", "COURSEKEYCONSTRAINT", "COURSE_KEY", + "SCHEMAGENMKJCCOURSE", "COURSEID"); + assertForeignKey("SCHEMAGENMKJC_RETAKES", "COURSEKEYSCONSTRAINT", "COURSE_KEY", + "SCHEMAGENMKJCCOURSE", "COURSEID"); + + // The foreign key constraints must be enforced: the database must reject a join + // table row whose map key has no matching row in the map key entity table. + assertSqlRejected("INSERT INTO SCHEMAGENMKJC_ENROLLMENTS (STUDENT_ID, SEMESTER_ID, COURSE_KEY) VALUES (1, 1, 99)"); + assertSqlRejected("INSERT INTO SCHEMAGENMKJC_RETAKES (STUDENT_ID, SEMESTER_ID, COURSE_KEY) VALUES (1, 1, 99)"); + }, "SCHEMAGENMKJCCOURSE", "SCHEMAGENMKJCSEMESTER", "SCHEMAGENMKJCSTUDENT", "SCHEMAGENMKJC_ENROLLMENTS", "SCHEMAGENMKJC_RETAKES"); + } + + /** + * Returns a map from each course id to the id of the semester it is mapped to. + */ + private static Map semesterIdByCourseId(Map map) { + return map.entrySet().stream() + .collect(Collectors.toMap(e -> e.getKey().getId(), e -> e.getValue().getId())); + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/mapKeyJoinColumn/Semester.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/mapKeyJoinColumn/Semester.java new file mode 100644 index 000000000..07ed2aea2 --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/mapKeyJoinColumn/Semester.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.annotations.mapKeyJoinColumn; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +@Entity +@Table(name = "SCHEMAGENMKJCSEMESTER") +public class Semester implements java.io.Serializable { + + private static final long serialVersionUID = 22L; + + @Id + @Column(name = "SEMESTERID") + private int id; + + @Column(name = "SEMESTERNAME") + private String name; + + public Semester() { + } + + public Semester(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + return id == ((Semester) o).id; + } + + @Override + public int hashCode() { + return id; + } + + @Override + public String toString() { + return getClass().getSimpleName() + "[id: " + id + "]"; + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/mapKeyJoinColumn/Student.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/mapKeyJoinColumn/Student.java new file mode 100644 index 000000000..7269bc10c --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/mapKeyJoinColumn/Student.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.annotations.mapKeyJoinColumn; + +import jakarta.persistence.Column; +import jakarta.persistence.ConstraintMode; +import jakarta.persistence.Entity; +import jakarta.persistence.ForeignKey; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinTable; +import jakarta.persistence.ManyToMany; +import jakarta.persistence.MapKeyJoinColumn; +import jakarta.persistence.MapKeyJoinColumns; +import jakarta.persistence.Table; + +import java.util.HashMap; +import java.util.Map; + +@Entity +@Table(name = "SCHEMAGENMKJCSTUDENT") +public class Student implements java.io.Serializable { + + private static final long serialVersionUID = 22L; + + @Id + @Column(name = "STUDENTID") + private int id; + + @ManyToMany + @JoinTable(name = "SCHEMAGENMKJC_ENROLLMENTS", + joinColumns = @JoinColumn(name = "STUDENT_ID"), + inverseJoinColumns = @JoinColumn(name = "SEMESTER_ID")) + @MapKeyJoinColumn(name = "COURSE_KEY", + foreignKey = @ForeignKey(name = "COURSEKEYCONSTRAINT", value = ConstraintMode.CONSTRAINT, + foreignKeyDefinition = "FOREIGN KEY (COURSE_KEY) REFERENCES SCHEMAGENMKJCCOURSE (COURSEID)")) + private Map enrollments = new HashMap<>(); + + @ManyToMany + @JoinTable(name = "SCHEMAGENMKJC_RETAKES", + joinColumns = @JoinColumn(name = "STUDENT_ID"), + inverseJoinColumns = @JoinColumn(name = "SEMESTER_ID")) + @MapKeyJoinColumns(value = @MapKeyJoinColumn(name = "COURSE_KEY"), + foreignKey = @ForeignKey(name = "COURSEKEYSCONSTRAINT", value = ConstraintMode.CONSTRAINT, + foreignKeyDefinition = "FOREIGN KEY (COURSE_KEY) REFERENCES SCHEMAGENMKJCCOURSE (COURSEID)")) + private Map retakes = new HashMap<>(); + + public Student() { + } + + public Student(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Map getEnrollments() { + return enrollments; + } + + public void setEnrollments(Map enrollments) { + this.enrollments = enrollments; + } + + public Map getRetakes() { + return retakes; + } + + public void setRetakes(Map retakes) { + this.retakes = retakes; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + return id == ((Student) o).id; + } + + @Override + public int hashCode() { + return id; + } + + @Override + public String toString() { + return getClass().getSimpleName() + "[id: " + id + "]"; + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/primaryKeyJoinColumn/Child.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/primaryKeyJoinColumn/Child.java new file mode 100644 index 000000000..f28ca340f --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/primaryKeyJoinColumn/Child.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.annotations.primaryKeyJoinColumn; + +import jakarta.persistence.Column; +import jakarta.persistence.ConstraintMode; +import jakarta.persistence.Entity; +import jakarta.persistence.ForeignKey; +import jakarta.persistence.PrimaryKeyJoinColumn; +import jakarta.persistence.Table; + +@Entity +@Table(name = "SCHEMAGENPKJCCHILD") +@PrimaryKeyJoinColumn(name = "CHILDID", referencedColumnName = "PARENTID", + foreignKey = @ForeignKey(name = "PKJCCONSTRAINT", value = ConstraintMode.CONSTRAINT, + foreignKeyDefinition = "FOREIGN KEY (CHILDID) REFERENCES SCHEMAGENPKJCPARENT (PARENTID)")) +public class Child extends Parent { + + private static final long serialVersionUID = 22L; + + @Column(name = "CHILDNAME") + private String childName; + + public Child() { + } + + public Child(int id, String childName) { + super(id); + this.childName = childName; + } + + public String getChildName() { + return childName; + } + + public void setChildName(String childName) { + this.childName = childName; + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/primaryKeyJoinColumn/Child2.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/primaryKeyJoinColumn/Child2.java new file mode 100644 index 000000000..2e9560f2e --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/primaryKeyJoinColumn/Child2.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.annotations.primaryKeyJoinColumn; + +import jakarta.persistence.Column; +import jakarta.persistence.ConstraintMode; +import jakarta.persistence.Entity; +import jakarta.persistence.ForeignKey; +import jakarta.persistence.PrimaryKeyJoinColumn; +import jakarta.persistence.PrimaryKeyJoinColumns; +import jakarta.persistence.Table; + +@Entity +@Table(name = "SCHEMAGENPKJCCHILD2") +@PrimaryKeyJoinColumns(value = @PrimaryKeyJoinColumn(name = "CHILD2ID", referencedColumnName = "PARENTID"), + foreignKey = @ForeignKey(name = "PKJCSCONSTRAINT", value = ConstraintMode.CONSTRAINT, + foreignKeyDefinition = "FOREIGN KEY (CHILD2ID) REFERENCES SCHEMAGENPKJCPARENT (PARENTID)")) +public class Child2 extends Parent { + + private static final long serialVersionUID = 22L; + + @Column(name = "CHILD2NAME") + private String child2Name; + + public Child2() { + } + + public Child2(int id, String child2Name) { + super(id); + this.child2Name = child2Name; + } + + public String getChild2Name() { + return child2Name; + } + + public void setChild2Name(String child2Name) { + this.child2Name = child2Name; + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/primaryKeyJoinColumn/Parent.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/primaryKeyJoinColumn/Parent.java new file mode 100644 index 000000000..627df43d1 --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/primaryKeyJoinColumn/Parent.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.annotations.primaryKeyJoinColumn; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.Table; + +@Entity +@Table(name = "SCHEMAGENPKJCPARENT") +@Inheritance(strategy = InheritanceType.JOINED) +public class Parent implements java.io.Serializable { + + private static final long serialVersionUID = 22L; + + @Id + @Column(name = "PARENTID") + private int id; + + public Parent() { + } + + public Parent(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + return id == ((Parent) o).id; + } + + @Override + public int hashCode() { + return id; + } + + @Override + public String toString() { + return getClass().getSimpleName() + "[id: " + id + "]"; + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/primaryKeyJoinColumn/SePrimaryKeyJoinColumnSchemaGenerationAnnotationClient.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/primaryKeyJoinColumn/SePrimaryKeyJoinColumnSchemaGenerationAnnotationClient.java new file mode 100644 index 000000000..3d4d247cb --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/annotations/primaryKeyJoinColumn/SePrimaryKeyJoinColumnSchemaGenerationAnnotationClient.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.annotations.primaryKeyJoinColumn; + +import ee.jakarta.tck.persistence.common.SchemaGenerationClientBase; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SePrimaryKeyJoinColumnSchemaGenerationAnnotationClient extends SchemaGenerationClientBase { + + private static final String TEST_CASE = "jpa_se_schemaGeneration_annotations_primaryKeyJoinColumn"; + + @Override + public JavaArchive createDeployment() throws Exception { + String packageName = SePrimaryKeyJoinColumnSchemaGenerationAnnotationClient.class.getPackageName(); + String[] classes = { + packageName + ".Parent", + packageName + ".Child", + packageName + ".Child2"}; + return createDeploymentJar(TEST_CASE + ".jar", packageName, classes); + } + + /** + * Verifies the {@code foreignKey} attribute of {@code @PrimaryKeyJoinColumn} + * and {@code @PrimaryKeyJoinColumns} is honored by schema generation for a + * {@code JOINED} inheritance hierarchy. The generated schema must contain the + * named foreign key constraints from the subclass tables to the root table, + * and the database must reject a subclass row without a matching root row. + */ + @Test + public void primaryKeyJoinColumnForeignKeyTest() throws Throwable { + verifyGeneratedSchema(() -> { + persist(new Child(1, "child"), new Child2(2, "child2")); + + var child = getEntityManager().find(Child.class, 1); + assertEquals("child", child.getChildName()); + var child2 = getEntityManager().find(Child2.class, 2); + assertEquals("child2", child2.getChild2Name()); + + // The foreign key constraints must exist in the database metadata under the + // names given in the mapping, on the subclass primary key join columns, + // referencing the primary key of the root entity table. + assertForeignKey("SCHEMAGENPKJCCHILD", "PKJCCONSTRAINT", "CHILDID", "SCHEMAGENPKJCPARENT", "PARENTID"); + assertForeignKey("SCHEMAGENPKJCCHILD2", "PKJCSCONSTRAINT", "CHILD2ID", "SCHEMAGENPKJCPARENT", "PARENTID"); + + // The foreign key constraints must be enforced: the database must reject a + // subclass table row whose primary key has no matching row in the root table. + assertSqlRejected("INSERT INTO SCHEMAGENPKJCCHILD (CHILDID, CHILDNAME) VALUES (99, 'orphan')"); + assertSqlRejected("INSERT INTO SCHEMAGENPKJCCHILD2 (CHILD2ID, CHILD2NAME) VALUES (99, 'orphan')"); + }, "SCHEMAGENPKJCPARENT", "SCHEMAGENPKJCCHILD", "SCHEMAGENPKJCCHILD2"); + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/Course.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/Course.java new file mode 100644 index 000000000..7d3eecec3 --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/Course.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.xml.mapKeyJoinColumn; + +public class Course implements java.io.Serializable { + + private static final long serialVersionUID = 22L; + + private int id; + + private String name; + + public Course() { + } + + public Course(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + return id == ((Course) o).id; + } + + @Override + public int hashCode() { + return id; + } + + @Override + public String toString() { + return getClass().getSimpleName() + "[id: " + id + "]"; + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/SeMapKeyJoinColumnSchemaGenerationXmlClient.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/SeMapKeyJoinColumnSchemaGenerationXmlClient.java new file mode 100644 index 000000000..43e81ef6d --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/SeMapKeyJoinColumnSchemaGenerationXmlClient.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.xml.mapKeyJoinColumn; + +import ee.jakarta.tck.persistence.common.SchemaGenerationClientBase; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; + +import java.util.Map; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SeMapKeyJoinColumnSchemaGenerationXmlClient extends SchemaGenerationClientBase { + + private static final String TEST_CASE = "jpa_se_schemaGeneration_xml_mapKeyJoinColumn"; + + @Override + public JavaArchive createDeployment() throws Exception { + String packageName = SeMapKeyJoinColumnSchemaGenerationXmlClient.class.getPackageName(); + String[] classes = { + packageName + ".Course", + packageName + ".Semester", + packageName + ".Student"}; + return createDeploymentJar(TEST_CASE + ".jar", packageName, classes, new String[] {ORM_XML}); + } + + /** + * Verifies the {@code map-key-foreign-key} subelement of {@code many-to-many} + * in {@code orm.xml} is honored by schema generation. The generated schema + * must contain the named foreign key constraint from the map key join column + * of the join table to the map key entity table, and the database must reject + * a join table row whose map key has no matching entity row. + */ + @Test + public void mapKeyJoinColumnForeignKeyXmlTest() throws Throwable { + verifyGeneratedSchema(() -> { + var course1 = new Course(1, "course1"); + var course2 = new Course(2, "course2"); + var semester1 = new Semester(1, "semester1"); + var semester2 = new Semester(2, "semester2"); + var student = new Student(1); + student.setEnrollments(Map.of(course1, semester1, course2, semester2)); + persist(course1, course2, semester1, semester2, student); + + // After reloading, each course key must still map to the semester it was + // persisted with. Entities are compared by id because the provider may + // return proxies for the map keys and values. + var found = getEntityManager().find(Student.class, 1); + + var enrollments = semesterIdByCourseId(found.getEnrollments()); + assertEquals(2, enrollments.size()); + assertEquals(semester1.getId(), enrollments.get(course1.getId())); + assertEquals(semester2.getId(), enrollments.get(course2.getId())); + + // The foreign key constraint must exist in the database metadata under the + // name given in the mapping, on the map key join column of the join table, + // referencing the primary key of the map key entity table. + assertForeignKey("SCHEMAGENXMLMKJC_ENROLLMENTS", "XMLCOURSEKEYCONSTRAINT", "COURSE_KEY", + "SCHEMAGENXMLMKJCCOURSE", "COURSEID"); + + // The foreign key constraint must be enforced: the database must reject a join + // table row whose map key has no matching row in the map key entity table. + assertSqlRejected("INSERT INTO SCHEMAGENXMLMKJC_ENROLLMENTS (STUDENT_ID, SEMESTER_ID, COURSE_KEY) VALUES (1, 1, 99)"); + }, "SCHEMAGENXMLMKJCCOURSE", "SCHEMAGENXMLMKJCSEMESTER", "SCHEMAGENXMLMKJCSTUDENT", "SCHEMAGENXMLMKJC_ENROLLMENTS"); + } + + /** + * Returns a map from each course id to the id of the semester it is mapped to. + */ + private static Map semesterIdByCourseId(Map map) { + return map.entrySet().stream() + .collect(Collectors.toMap(e -> e.getKey().getId(), e -> e.getValue().getId())); + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/Semester.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/Semester.java new file mode 100644 index 000000000..61fe5e54c --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/Semester.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.xml.mapKeyJoinColumn; + +public class Semester implements java.io.Serializable { + + private static final long serialVersionUID = 22L; + + private int id; + + private String name; + + public Semester() { + } + + public Semester(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + return id == ((Semester) o).id; + } + + @Override + public int hashCode() { + return id; + } + + @Override + public String toString() { + return getClass().getSimpleName() + "[id: " + id + "]"; + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/Student.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/Student.java new file mode 100644 index 000000000..5c8d35c8c --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/Student.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.xml.mapKeyJoinColumn; + +import java.util.HashMap; +import java.util.Map; + +public class Student implements java.io.Serializable { + + private static final long serialVersionUID = 22L; + + private int id; + + private Map enrollments = new HashMap<>(); + + public Student() { + } + + public Student(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Map getEnrollments() { + return enrollments; + } + + public void setEnrollments(Map enrollments) { + this.enrollments = enrollments; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + return id == ((Student) o).id; + } + + @Override + public int hashCode() { + return id; + } + + @Override + public String toString() { + return getClass().getSimpleName() + "[id: " + id + "]"; + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/Child.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/Child.java new file mode 100644 index 000000000..3382946fc --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/Child.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.xml.primaryKeyJoinColumn; + +public class Child extends Parent { + + private static final long serialVersionUID = 22L; + + private String childName; + + public Child() { + } + + public Child(int id, String childName) { + super(id); + this.childName = childName; + } + + public String getChildName() { + return childName; + } + + public void setChildName(String childName) { + this.childName = childName; + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/Child2.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/Child2.java new file mode 100644 index 000000000..0f7c6fca6 --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/Child2.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.xml.primaryKeyJoinColumn; + +public class Child2 extends Parent { + + private static final long serialVersionUID = 22L; + + private String child2Name; + + public Child2() { + } + + public Child2(int id, String child2Name) { + super(id); + this.child2Name = child2Name; + } + + public String getChild2Name() { + return child2Name; + } + + public void setChild2Name(String child2Name) { + this.child2Name = child2Name; + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/Parent.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/Parent.java new file mode 100644 index 000000000..5b77add69 --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/Parent.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.xml.primaryKeyJoinColumn; + +public class Parent implements java.io.Serializable { + + private static final long serialVersionUID = 22L; + + private int id; + + public Parent() { + } + + public Parent(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + return id == ((Parent) o).id; + } + + public int hashCode() { + return id; + } + + public String toString() { + return getClass().getSimpleName() + "[id: " + id + "]"; + } +} diff --git a/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/SePrimaryKeyJoinColumnSchemaGenerationXmlClient.java b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/SePrimaryKeyJoinColumnSchemaGenerationXmlClient.java new file mode 100644 index 000000000..2ee87c180 --- /dev/null +++ b/tck/spec-tests/src/main/java/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/SePrimaryKeyJoinColumnSchemaGenerationXmlClient.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * + * 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. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package ee.jakarta.tck.persistence.se.schemaGeneration.xml.primaryKeyJoinColumn; + +import ee.jakarta.tck.persistence.common.SchemaGenerationClientBase; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SePrimaryKeyJoinColumnSchemaGenerationXmlClient extends SchemaGenerationClientBase { + + private static final String TEST_CASE = "jpa_se_schemaGeneration_xml_primaryKeyJoinColumn"; + + @Override + public JavaArchive createDeployment() throws Exception { + String packageName = SePrimaryKeyJoinColumnSchemaGenerationXmlClient.class.getPackageName(); + String[] classes = { + packageName + ".Parent", + packageName + ".Child", + packageName + ".Child2"}; + return createDeploymentJar(TEST_CASE + ".jar", packageName, classes, new String[] {ORM_XML}); + } + + /** + * Verifies the {@code primary-key-foreign-key} subelement of {@code entity} + * and the {@code foreign-key} subelement of {@code primary-key-join-column} + * in {@code orm.xml} are honored by schema generation for a {@code JOINED} + * inheritance hierarchy. The generated schema must contain the named foreign + * key constraints from the subclass tables to the root table, and the + * database must reject a subclass row without a matching root row. + */ + @Test + public void primaryKeyJoinColumnForeignKeyXmlTest() throws Throwable { + verifyGeneratedSchema(() -> { + persist(new Child(1, "child"), new Child2(2, "child2")); + + var child = getEntityManager().find(Child.class, 1); + assertEquals("child", child.getChildName()); + var child2 = getEntityManager().find(Child2.class, 2); + assertEquals("child2", child2.getChild2Name()); + + // The foreign key constraints must exist in the database metadata under the + // names given in the mapping, on the subclass primary key join columns, + // referencing the primary key of the root entity table. + assertForeignKey("SCHEMAGENXMLPKJCCHILD", "XMLPKJCCONSTRAINT", "CHILDID", "SCHEMAGENXMLPKJCPARENT", "PARENTID"); + assertForeignKey("SCHEMAGENXMLPKJCCHILD2", "XMLPKJCSCONSTRAINT", "CHILD2ID", "SCHEMAGENXMLPKJCPARENT", "PARENTID"); + + // The foreign key constraints must be enforced: the database must reject a + // subclass table row whose primary key has no matching row in the root table. + assertSqlRejected("INSERT INTO SCHEMAGENXMLPKJCCHILD (CHILDID, CHILDNAME) VALUES (99, 'orphan')"); + assertSqlRejected("INSERT INTO SCHEMAGENXMLPKJCCHILD2 (CHILD2ID, CHILD2NAME) VALUES (99, 'orphan')"); + }, "SCHEMAGENXMLPKJCPARENT", "SCHEMAGENXMLPKJCCHILD", "SCHEMAGENXMLPKJCCHILD2"); + } +} diff --git a/tck/spec-tests/src/main/resources/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/orm.xml b/tck/spec-tests/src/main/resources/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/orm.xml new file mode 100644 index 000000000..fedc38ffc --- /dev/null +++ b/tck/spec-tests/src/main/resources/ee/jakarta/tck/persistence/se/schemaGeneration/xml/mapKeyJoinColumn/orm.xml @@ -0,0 +1,64 @@ + + + + + ee.jakarta.tck.persistence.se.schemaGeneration.xml.mapKeyJoinColumn + + + + + + + + + + + + +
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + diff --git a/tck/spec-tests/src/main/resources/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/orm.xml b/tck/spec-tests/src/main/resources/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/orm.xml new file mode 100644 index 000000000..2e644b83b --- /dev/null +++ b/tck/spec-tests/src/main/resources/ee/jakarta/tck/persistence/se/schemaGeneration/xml/primaryKeyJoinColumn/orm.xml @@ -0,0 +1,57 @@ + + + + + ee.jakarta.tck.persistence.se.schemaGeneration.xml.primaryKeyJoinColumn + +
+ + + + + + + + +
+ + + + + + + + + +
+ + + + + + + + + +