Skip to content

Add TCK tests for foreignKey schema generation on @PrimaryKeyJoinColumn and @MapKeyJoinColumn - #1225

Merged
gavinking merged 2 commits into
jakartaee:mainfrom
dreab8:tck_primaryKeyJoinColumnFK_mapKeyJoinColumnFK
Nov 24, 2026
Merged

gavinking merged 2 commits into
jakartaee:mainfrom
dreab8:tck_primaryKeyJoinColumnFK_mapKeyJoinColumnFK

Conversation

@dreab8

@dreab8 dreab8 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Add Jakarta Persistence TCK tests checking that schema generation honors the foreignKey attribute. Two cases are covered:

  • Primary key join columns: @PrimaryKeyJoinColumn and @PrimaryKeyJoinColumns in a JOINED inheritance hierarchy. In orm.xml, these is <primary-key-foreign-key>.
  • Map key join columns: @MapKeyJoinColumn and @MapKeyJoinColumns on a @ManyToMany whose map key is an entity. In orm.xml, this is <map-key-foreign-key>.

Each test does the following:

  1. Generates the create and drop scripts.
  2. Checks that the create script contains the named foreign key constraints.
  3. Checks that the drop script removes them, through either ALTER TABLE ... DROP or DROP TABLE ... CASCADE.
  4. Runs both scripts against the database, persisting and reloading the entities in between to confirm the generated schema works.

Both the annotation and the orm.xml mappings are tested.

Comment on lines +83 to +140
public void primaryKeyJoinColumnForeignKeyXmlTest() throws Exception {
var createScript = new File(schemaGenerationDir + File.separator + "create_" + TEST_CASE + ".sql");
var dropScript = new File(schemaGenerationDir + File.separator + "drop_" + TEST_CASE + ".sql");
deleteItem(createScript);
deleteItem(dropScript);

// Step 1: only write the create and drop scripts, nothing is executed against the database.
var props = getPersistenceUnitProperties();
props.put("jakarta.persistence.schema-generation.database.action", "none");
props.put("jakarta.persistence.schema-generation.scripts.action", "drop-and-create");
props.put("jakarta.persistence.schema-generation.create-database-schemas", "false");
props.put("jakarta.persistence.schema-generation.scripts.create-target", convertToURI(createScript.getPath()));
props.put("jakarta.persistence.schema-generation.scripts.drop-target", convertToURI(dropScript.getPath()));
Persistence.createEntityManagerFactory(getPersistenceUnitName(), props).close();

// Step 2: the create script must contain the tables and the named foreign keys from each subclass
// table to the root table. The trailing space in "...CHILD " prevents also matching the CHILD2 table.
assertAll("create script",
() -> assertTrue(findDataInFile(createScript,
List.of("CREATE TABLE SCHEMAGENXMLPKJCPARENT", "PARENTID", "PRIMARY KEY (PARENTID)")),
"SCHEMAGENXMLPKJCPARENT table not created"),
() -> assertTrue(findDataInFile(createScript,
List.of("CREATE TABLE SCHEMAGENXMLPKJCCHILD ", "CHILDID", "CHILDNAME", "PRIMARY KEY (CHILDID)")),
"SCHEMAGENXMLPKJCCHILD table not created"),
() -> assertTrue(findDataInFile(createScript,
List.of("CREATE TABLE SCHEMAGENXMLPKJCCHILD2", "CHILD2ID", "CHILD2NAME", "PRIMARY KEY (CHILD2ID)")),
"SCHEMAGENXMLPKJCCHILD2 table not created"),
() -> assertTrue(findDataInFile(createScript,
List.of("ALTER TABLE", "SCHEMAGENXMLPKJCCHILD ADD",
"CONSTRAINT XMLPKJCCONSTRAINT FOREIGN KEY (CHILDID) REFERENCES SCHEMAGENXMLPKJCPARENT (PARENTID)")),
"<primary-key-foreign-key> foreign key XMLPKJCCONSTRAINT not created"),
() -> assertTrue(findDataInFile(createScript,
List.of("ALTER TABLE", "SCHEMAGENXMLPKJCCHILD2 ADD",
"CONSTRAINT XMLPKJCSCONSTRAINT FOREIGN KEY (CHILD2ID) REFERENCES SCHEMAGENXMLPKJCPARENT (PARENTID)")),
"<primary-key-join-column><foreign-key> foreign key XMLPKJCSCONSTRAINT not created"));

// Step 3: the drop script must remove each foreign key, either explicitly (ALTER TABLE ... DROP)
// or implicitly (DROP TABLE ... CASCADE), both are valid, and must drop the tables.
assertAll("drop script",
() -> assertTrue(findDataInFile(dropScript, List.of("ALTER TABLE", "SCHEMAGENXMLPKJCCHILD DROP"))
|| findDataInFile(dropScript, List.of("DROP TABLE", "SCHEMAGENXMLPKJCCHILD ", "CASCADE")),
"SCHEMAGENXMLPKJCCHILD foreign key not dropped"),
() -> assertTrue(findDataInFile(dropScript, List.of("ALTER TABLE", "SCHEMAGENXMLPKJCCHILD2 DROP"))
|| findDataInFile(dropScript, List.of("DROP TABLE", "SCHEMAGENXMLPKJCCHILD2", "CASCADE")),
"SCHEMAGENXMLPKJCCHILD2 foreign key not dropped"),
() -> assertTrue(findDataInFile(dropScript, List.of("DROP TABLE", "SCHEMAGENXMLPKJCPARENT")),
"SCHEMAGENXMLPKJCPARENT table not dropped"),
() -> assertTrue(findDataInFile(dropScript, List.of("DROP TABLE", "SCHEMAGENXMLPKJCCHILD2")),
"SCHEMAGENXMLPKJCCHILD2 table not dropped"));

// Step 4: execute the generated create script against the database.
props = getPersistenceUnitProperties();
props.put("jakarta.persistence.schema-generation.database.action", "create");
props.put("jakarta.persistence.schema-generation.scripts.action", "none");
props.put("jakarta.persistence.schema-generation.create-database-schemas", "true");
props.put("jakarta.persistence.schema-generation.create-script-source", convertToURI(createScript.getPath()));
Persistence.generateSchema(getPersistenceUnitName(), props);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dreab8 The TCK should never make assertions about generated SQL.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @gavinking I agree that asserting against generated SQL may not be ideal. I'm trying to find an alternative way to verify this behavior, but in the meantime, I followed the approach used by existing tests in the schemaGeneration/annotations package, which perform similar checks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dreab8 have a look at what I did with #1226

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

@dreab8
dreab8 force-pushed the tck_primaryKeyJoinColumnFK_mapKeyJoinColumnFK branch from b321b38 to 063f40e Compare November 23, 2026 13:51
@dreab8

dreab8 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Hi @gavinking I’ve just updated my PR to leverage the work you’ve done in SchemaGenerationClientBase.Hopefully, I’ve done it the right way!

Comment on lines +62 to +63
assertEquals(Map.of(course1, semester1, course2, semester2), Map.copyOf(found.getEnrollments()));
assertEquals(Map.of(course1, semester2), Map.copyOf(found.getRetakes()));

@gavinking gavinking Nov 23, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Astra complains that:


Could we compare the Course.getId() → Semester.getId() pairs here and in SeMapKeyJoinColumnSchemaGenerationXmlClient? The persist() helper clears the persistence context, so these assertions compare detached instances with reloaded map entries. Both Course.equals() and Semester.equals() require the same runtime class. A provider may return a subclass proxy for a map key or value, which would make a correct mapping fail this test. Comparing the ID pairs still verifies which semester belongs to each course.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gavinking ,changes applied.

Cover @PrimaryKeyJoinColumn.foreignKey and @PrimaryKeyJoinColumns.foreignKey,
and the orm.xml equivalents primary-key-foreign-key and the foreign-key
subelement of primary-key-join-column.
@dreab8
dreab8 force-pushed the tck_primaryKeyJoinColumnFK_mapKeyJoinColumnFK branch from 063f40e to f4cefef Compare November 24, 2026 09:10
@gavinking
gavinking merged commit 679f658 into jakartaee:main Nov 24, 2026
3 checks passed
@gavinking

Copy link
Copy Markdown
Member

Thank you @dreab8 🙏 much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants