Skip to content

Conversation

fanste
Copy link

@fanste fanste commented Jun 13, 2024

Foreign keys based on compound keys run into a check for java records. But the to-be-generated classes do not exist on the classpath and therefore the check fails.

Issue: https://hibernate.atlassian.net/browse/HHH-18228

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.

@fanste
Copy link
Author

fanste commented Jul 4, 2024

Any feedback on this?

@hibernate-github-bot
Copy link

hibernate-github-bot bot commented Jul 17, 2024

Thanks for your pull request!

This pull request appears to follow the contribution rules.

› This message was automatically generated.

Foreign keys based on compound keys run into a check for java records.
But the to-be-generated classes do not exist on the classpath and
therefore the check fails.

`org.hibernate.mapping.Component#getComponentClass` is now used in a
lazy manner
@fanste
Copy link
Author

fanste commented Sep 20, 2024

Hibernate doesn't seem to be dead... So the project is simply not interested in community contributions?! Would be too sad, if no solution will be implemented for that problem...

Copy link
Member

@mbellade mbellade left a comment

Choose a reason for hiding this comment

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

@fanste thank you for your contribution! Sorry for taking so long, we have a lot of issues in our backlog and we're doing our best to review pending PRs. Left a comment for you.

We're also in the process or investigating a move to the Apache Software License v2. If you agree with allowing this change to be relicensed under that license, please add this text to the subject -

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.

Comment on lines +91 to +95
// As mutable and componentClass are based on the actual component
// type, both variables must be lazily initialized in some cases.
// E.g. on code generation by hibernate-tools, the component type
// might not yet exist (compound keys), resulting in a
// ClassNotFoundException.
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I fully understand this use-case: why would the component class, i.e. the class of the generated composite key, not exist while the componentClassName does not return null? Maybe the problem is we shouldn't return a class name at all, or we should resolve the class eagerly when generating the ComponentType.

Could you please include a test demonstrating the issue within your PR? This can help us understand the problem better and ensure no regressions will be introduced in the future.

Copy link
Author

Choose a reason for hiding this comment

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

The classes are also used by the hibernate-tool project for the code generation. So the to-be-generated XXXId class is present in the DB metadata (because to a compound key) and therefore in the class model but does not yet physically exist.

There is also an active hibernate-tools issue for this: HBX-2840

But I will try to create a standalone reproducer.

Copy link
Member

Choose a reason for hiding this comment

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

There is also an active hibernate-tools issue for this: HBX-2840

Great, hopefully Koen will be able to take a look soon and if we can identify this as a tools issue I'm sure it will be fixed. If you want, you can also try taking a look and proposing a fix, if possible.

Let us know about the test. If you don't mean to continue contributing through this PR, please close it so we can progress with cleanup in our backlog.

Copy link
Author

Choose a reason for hiding this comment

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

I doubt it is a pure TOOLS issue, as the second-pass processing is setup and run by ORM, based on the metadata feed via TOOLS. See this line in mapping.BasicValue.
Either TOOLS is not allowed to use the classes from CORE anymore - which would be a huge pain in the a.. (i think) or this use case needs to be handled by ORM.

This commit is the root cause of this issue: 3e8056d

Reproducer is attaches as a separat comment.

@fanste
Copy link
Author

fanste commented Dec 16, 2024

Reproducer: https://github.com/fanste/reproducers/tree/hhh-18228

Run it using the command mvn generate-sources.

This should throw the following error:

[ERROR] Failed to execute goal org.hibernate.tool:hibernate-tools-maven:6.6.2.Final:hbm2java (entity-generation) on project hhh-18228: Execution entity-generation of goal org.hibernate.tool:hibernate-tools-maven:6.6.2.Final:hbm2java failed: component class not found: Tbltest1Id

Reason is the compound primary key of tblTest1 also used as a foreign key in tblTest2. The demonstrated use case is simplied to the bare minimum.

@fanste
Copy link
Author

fanste commented Mar 10, 2025

Fixed by HBX-2840 / hibernate-tools#4986

@fanste fanste closed this Mar 10, 2025
@fanste fanste deleted the HHH-18228 branch March 10, 2025 16:16
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