Repository navigation
Add TCK tests for foreignKey schema generation on @PrimaryKeyJoinColumn and @MapKeyJoinColumn - #1225
Conversation
| 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); | ||
|
|
There was a problem hiding this comment.
@dreab8 The TCK should never make assertions about generated SQL.
There was a problem hiding this comment.
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.
b321b38 to
063f40e
Compare
|
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! |
| assertEquals(Map.of(course1, semester1, course2, semester2), Map.copyOf(found.getEnrollments())); | ||
| assertEquals(Map.of(course1, semester2), Map.copyOf(found.getRetakes())); |
There was a problem hiding this comment.
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.
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.
063f40e to
f4cefef
Compare
|
Thank you @dreab8 🙏 much appreciated! |
Add Jakarta Persistence TCK tests checking that schema generation honors the foreignKey attribute. Two cases are covered:
@PrimaryKeyJoinColumnand@PrimaryKeyJoinColumnsin a JOINED inheritance hierarchy. In orm.xml, these is<primary-key-foreign-key>.@MapKeyJoinColumnand@MapKeyJoinColumnson a@ManyToManywhose map key is an entity. In orm.xml, this is<map-key-foreign-key>.Each test does the following:
Both the annotation and the orm.xml mappings are tested.