Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-18714 add support for inheritance in entity graph #9088

Open
wants to merge 1 commit into
base: 6.6
Choose a base branch
from

Conversation

Asanio06
Copy link

[Please describe here what your change is about]
This change enables support for inheritance with EntityGraphs as described in the JPA specification.
I've added some functions such as addTreatedSubgraph to facilitate future integration with Hibernate 7 (Jpa 3.2).
The ticket related to my proposal: https://hibernate.atlassian.net/browse/HHH-18714


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.


@Asanio06
Copy link
Author

Asanio06 commented Oct 14, 2024

@gavinking I don't understand why h2 build fail. Do I need to relaunch the build for h2? Is it possible that this is coming from scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); which breaks h2 ?

Edit: Its work now

@gavinking
Copy link
Member

@gavinking I don't understand why h2 build fail. Do I need to relaunch the build for h2? Is it possible that this is coming from scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); which breaks h2 ?

Edit: Its work now

Maybe I had spaces instead of tabs in my suggestion.

@Asanio06
Copy link
Author

@gavinking I don't understand why h2 build fail. Do I need to relaunch the build for h2? Is it possible that this is coming from scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); which breaks h2 ?
Edit: Its work now

Maybe I had spaces instead of tabs in my suggestion.

I think it was me, I had forgotten some unused imports.

@Asanio06
Copy link
Author

Hello @gavinking What do you think of this change ? Should I plan to add documentation on how it works, or is it too risky ?

@sebersole sebersole self-requested a review October 18, 2024 14:00
this.name = name;
this.subclassSubgraphs = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

This should IMO be instantiated lazily.

PersistentAttribute<?, ?> attribute = managedType.findAttributeInSuperTypes( attributeName );

if ( attribute == null ) {
attribute = managedType.findSubTypesAttribute( attributeName );
Copy link
Contributor

Choose a reason for hiding this comment

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

Please revert this. Implicitly looking into subtypes is not an option. Users will have to explicitly cast the graph to "treat" it and can then access subtype attributes.

Comment on lines +74 to +78
SubGraphImplementor<S> subgraph = new SubGraphImpl<>( subTypeEntityDomainType, true );

subclassSubgraphs.putIfAbsent( subTypeEntityDomainType, subgraph );

return subgraph;
Copy link
Contributor

Choose a reason for hiding this comment

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

The sub-graph needs to know about this since the javadoc of addTreatedSubgraph says:

Subclass subgraphs automatically include the specified attributes of superclass subgraphs.

also, I think it's desirable to return an existing instance:

Suggested change
SubGraphImplementor<S> subgraph = new SubGraphImpl<>( subTypeEntityDomainType, true );
subclassSubgraphs.putIfAbsent( subTypeEntityDomainType, subgraph );
return subgraph;
final SubGraphImplementor<?> existingSubgraph = subclassSubgraphs.get( subTypeEntityDomainType );
if ( existingSubgraph != null ) {
return existingSubgraph;
}
final SubGraphImplementor<S> subgraph = new SubGraphImpl<>( subTypeEntityDomainType, true );
subclassSubgraphs.put( subTypeEntityDomainType, subgraph );
return subgraph;

);
}
if ( StringHelper.isNotEmpty( namedAttributeNode.keySubgraph() ) ) {
final SubGraphImplementor<?> subgraph = attributeNode.makeKeySubGraph();
Copy link
Contributor

Choose a reason for hiding this comment

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

Using this method to create a sub-graph for a map-key is vital. This change won't work if a key subgraph uses a subtype subgraph with the same type as a value subgraph.

GraphImplementor mergedSubgraph = new SubGraphImpl<>( managedDomainType, true );

for ( GraphImplementor subgraph : subgraphMap.values() ) {
mergedSubgraph.merge( subgraph );
Copy link
Contributor

Choose a reason for hiding this comment

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

This will not be necessary anymore once the SubGraphImpl knows of its parent like the spec requires it. See my previous comment.

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.

3 participants