HHH-17377 - Migrate to JPA 3.2

https://hibernate.atlassian.net/browse/HHH-17377

Just Graph completed - 2 left related to Session/EntityManager
This commit is contained in:
Steve Ebersole 2023-11-01 19:40:31 -05:00
parent 1e110584f1
commit be0fc9ee6a
11 changed files with 190 additions and 70 deletions

View File

@ -8,6 +8,9 @@ package org.hibernate.graph;
import java.util.List;
import jakarta.persistence.Subgraph;
import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.MapAttribute;
import jakarta.persistence.metamodel.PluralAttribute;
import org.hibernate.metamodel.model.domain.ManagedDomainType;
import org.hibernate.metamodel.model.domain.PersistentAttribute;
@ -29,7 +32,7 @@ import org.hibernate.metamodel.model.domain.PersistentAttribute;
* @see jakarta.persistence.EntityGraph
* @see jakarta.persistence.Subgraph
*/
public interface Graph<J> extends GraphNode<J> {
public interface Graph<J> extends GraphNode<J>, jakarta.persistence.Graph<J> {
/**
* Add a subgraph rooted at a plural attribute, allowing further
@ -41,7 +44,7 @@ public interface Graph<J> extends GraphNode<J> {
*
* @since 6.3
*/
default <AJ> SubGraph<AJ> addPluralSubgraph(PluralAttribute<? extends J, ?, AJ> attribute) {
default <AJ> SubGraph<AJ> addPluralSubgraph(PluralAttribute<? super J, ?, AJ> attribute) {
return addSubGraph( attribute.getName() );
}
@ -92,24 +95,18 @@ public interface Graph<J> extends GraphNode<J> {
* Find an already existing AttributeNode by corresponding attribute
* reference, within this container.
*/
<AJ> AttributeNode<AJ> findAttributeNode(PersistentAttribute<? extends J, AJ> attribute);
<AJ> AttributeNode<AJ> findAttributeNode(PersistentAttribute<? super J, AJ> attribute);
/**
* Get a list of all existing AttributeNodes within this container.
*/
List<AttributeNode<?>> getAttributeNodeList();
/**
* Add an {@link AttributeNode} (with no associated {@link SubGraph})
* to this container by attribute name.
*/
<AJ> AttributeNode<AJ> addAttributeNode(String attributeName);
/**
* Add an {@link AttributeNode} (with no associated {@link SubGraph})
* to this container by attribute reference.
*/
<AJ> AttributeNode<AJ> addAttributeNode(PersistentAttribute<? extends J,AJ> attribute);
<AJ> AttributeNode<AJ> addAttributeNode(PersistentAttribute<? super J,AJ> attribute);
@ -134,10 +131,10 @@ public interface Graph<J> extends GraphNode<J> {
*
* @apiNote If no such AttributeNode exists yet, it is created.
*/
<AJ> SubGraph<AJ> addSubGraph(PersistentAttribute<? extends J, AJ> attribute)
<AJ> SubGraph<AJ> addSubGraph(PersistentAttribute<? super J, AJ> attribute)
throws CannotContainSubGraphException;
<AJ> SubGraph<? extends AJ> addSubGraph(PersistentAttribute<? extends J, AJ> attribute, Class<? extends AJ> type)
<AJ> SubGraph<? extends AJ> addSubGraph(PersistentAttribute<? super J, AJ> attribute, Class<? extends AJ> type)
throws CannotContainSubGraphException;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -148,8 +145,18 @@ public interface Graph<J> extends GraphNode<J> {
<AJ> SubGraph<AJ> addKeySubGraph(String attributeName, Class<AJ> type)
throws CannotContainSubGraphException;
<AJ> SubGraph<AJ> addKeySubGraph(PersistentAttribute<? extends J,AJ> attribute)
<AJ> SubGraph<AJ> addKeySubGraph(PersistentAttribute<? super J,AJ> attribute)
throws CannotContainSubGraphException;
<AJ> SubGraph<? extends AJ> addKeySubGraph(PersistentAttribute<? extends J,AJ> attribute, Class<? extends AJ> type)
<AJ> SubGraph<? extends AJ> addKeySubGraph(PersistentAttribute<? super J,AJ> attribute, Class<? extends AJ> type)
throws CannotContainSubGraphException;
@Override
<Y> SubGraph<Y> addTreatedSubgraph(Attribute<? super J, ? super Y> attribute, Class<Y> type);
@Override
<E> SubGraph<E> addTreatedElementSubgraph(PluralAttribute<? super J, ?, ? super E> attribute, Class<E> type);
@Override
<K> SubGraph<K> addTreatedMapKeySubgraph(MapAttribute<? super J, ? super K, ?> attribute, Class<K> type);
}

View File

@ -48,23 +48,13 @@ public interface RootGraph<J> extends Graph<J>, EntityGraph<J> {
}
@Override
@SuppressWarnings("unchecked")
default void addAttributeNodes(Attribute<J, ?>... attributes) {
if ( attributes != null ) {
for ( Attribute<J, ?> attribute : attributes ) {
addAttributeNode( (PersistentAttribute<J,?>) attribute );
}
}
default <X> SubGraph<X> addSubgraph(Attribute<? super J, X> attribute) {
return addSubGraph( (PersistentAttribute<? super J,X>) attribute );
}
@Override
default <X> SubGraph<X> addSubgraph(Attribute<J, X> attribute) {
return addSubGraph( (PersistentAttribute<J,X>) attribute );
}
@Override
default <X> SubGraph<? extends X> addSubgraph(Attribute<J, X> attribute, Class<? extends X> type) {
return addSubGraph( (PersistentAttribute<J,X>) attribute, type );
default <X> Subgraph<? extends X> addSubgraph(Attribute<? super J, X> attribute, Class<? extends X> type) {
return addSubGraph( (PersistentAttribute<? super J,X>) attribute, type );
}
@Override
@ -78,13 +68,13 @@ public interface RootGraph<J> extends Graph<J>, EntityGraph<J> {
}
@Override
default <X> SubGraph<X> addKeySubgraph(Attribute<J, X> attribute) {
return addKeySubGraph( (PersistentAttribute<J,X>) attribute );
default <X> SubGraph<X> addKeySubgraph(Attribute<? super J, X> attribute) {
return addKeySubGraph( (PersistentAttribute<? super J,X>) attribute );
}
@Override
default <X> SubGraph<? extends X> addKeySubgraph(Attribute<J, X> attribute, Class<? extends X> type) {
return addKeySubGraph( (PersistentAttribute<J,X>) attribute, type );
default <X> SubGraph<? extends X> addKeySubgraph(Attribute<? super J, X> attribute, Class<? extends X> type) {
return addKeySubGraph( (PersistentAttribute<? super J,X>) attribute, type );
}
@Override

View File

@ -40,25 +40,24 @@ public interface SubGraph<J> extends Graph<J>, Subgraph<J> {
}
@Override
@SuppressWarnings("unchecked")
default void addAttributeNodes(Attribute<J, ?>... attribute) {
default void addAttributeNodes(Attribute<? super J, ?>... attribute) {
if ( attribute == null ) {
return;
}
for ( Attribute<J, ?> node : attribute ) {
for ( Attribute<? super J, ?> node : attribute ) {
assert node instanceof PersistentAttribute;
addAttributeNode( (PersistentAttribute<J, ?>) node );
addAttributeNode( node );
}
}
@Override
default <X> SubGraph<X> addSubgraph(Attribute<J, X> attribute) {
return addSubGraph( (PersistentAttribute<J, X>) attribute );
default <X> SubGraph<X> addSubgraph(Attribute<? super J, X> attribute) {
return addSubGraph( (PersistentAttribute<? super J, X>) attribute );
}
@Override
default <X> SubGraph<? extends X> addSubgraph(Attribute<J, X> attribute, Class<? extends X> type) {
default <X> SubGraph<? extends X> addSubgraph(Attribute<? super J, X> attribute, Class<? extends X> type) {
return addSubGraph( (PersistentAttribute<J, X>) attribute, type );
}
@ -75,13 +74,13 @@ public interface SubGraph<J> extends Graph<J>, Subgraph<J> {
@Override
default <X> SubGraph<X> addKeySubgraph(Attribute<J, X> attribute) {
return addKeySubGraph( (PersistentAttribute<J, X>) attribute );
default <X> SubGraph<X> addKeySubgraph(Attribute<? super J, X> attribute) {
return addKeySubGraph( (PersistentAttribute<? super J, X>) attribute );
}
@Override
default <X> SubGraph<? extends X> addKeySubgraph(Attribute<J, X> attribute, Class<? extends X> type) {
return addKeySubGraph( (PersistentAttribute<J, X>) attribute, type );
default <X> SubGraph<? extends X> addKeySubgraph(Attribute<? super J, X> attribute, Class<? extends X> type) {
return addKeySubGraph( (PersistentAttribute<? super J, X>) attribute, type );
}
@Override

View File

@ -25,6 +25,8 @@ import org.hibernate.metamodel.model.domain.ManagedDomainType;
import org.hibernate.metamodel.model.domain.PersistentAttribute;
import org.hibernate.query.sqm.SqmPathSource;
import jakarta.persistence.metamodel.Attribute;
import static java.util.Collections.emptyList;
/**
@ -77,7 +79,7 @@ public abstract class AbstractGraph<J> extends AbstractGraphNode<J> implements G
for ( AttributeNodeImplementor<?> attributeNode : other.getAttributeNodeImplementors() ) {
final AttributeNodeImplementor<?> localAttributeNode = findAttributeNode(
(PersistentAttribute<? extends J,?>) attributeNode.getAttributeDescriptor()
(PersistentAttribute<? super J,?>) attributeNode.getAttributeDescriptor()
);
if ( localAttributeNode != null ) {
// keep the local one, but merge in the incoming one
@ -128,12 +130,12 @@ public abstract class AbstractGraph<J> extends AbstractGraphNode<J> implements G
if ( attribute instanceof SqmPathSource && ( (SqmPathSource<?>) attribute ).isGeneric() ) {
attribute = managedType.findConcreteGenericAttribute( attributeName );
}
return attribute == null ? null : findAttributeNode( (PersistentAttribute<? extends J, AJ>) attribute );
return attribute == null ? null : findAttributeNode( (PersistentAttribute<? super J, AJ>) attribute );
}
@Override
@SuppressWarnings("unchecked")
public <AJ> AttributeNodeImplementor<AJ> findAttributeNode(PersistentAttribute<? extends J, AJ> attribute) {
public <AJ> AttributeNodeImplementor<AJ> findAttributeNode(PersistentAttribute<? super J, AJ> attribute) {
return attrNodeMap == null ? null : (AttributeNodeImplementor<AJ>) attrNodeMap.get( attribute );
}
@ -149,20 +151,54 @@ public abstract class AbstractGraph<J> extends AbstractGraphNode<J> implements G
}
@Override
public <AJ> AttributeNodeImplementor<AJ> addAttributeNode(String attributeName)
throws CannotContainSubGraphException {
return findOrCreateAttributeNode( attributeName );
public void addAttributeNode(String attributeName) throws CannotContainSubGraphException {
findOrCreateAttributeNode( attributeName );
}
@Override
public <AJ> AttributeNodeImplementor<AJ> addAttributeNode(PersistentAttribute<? extends J, AJ> attribute)
public <AJ> AttributeNodeImplementor<AJ> addAttributeNode(PersistentAttribute<? super J, AJ> attribute)
throws CannotContainSubGraphException {
return findOrCreateAttributeNode( attribute );
}
@Override
public void addAttributeNode(Attribute<? super J, ?> attribute) {
findOrCreateAttributeNode( (PersistentAttribute<? super J, ?>) attribute );
}
@Override
public void addAttributeNodes(String... attributeNames) {
for ( int i = 0; i < attributeNames.length; i++ ) {
addAttributeNode( attributeNames[i] );
}
}
@Override
public void addAttributeNodes(Attribute<? super J, ?>... attributes) {
for ( int i = 0; i < attributes.length; i++ ) {
addAttributeNode( attributes[i] );
}
}
@Override
public void removeAttributeNode(String attributeName) {
final PersistentAttribute<? super J, ?> attribute = managedType.findAttribute( attributeName );
attrNodeMap.remove( attribute );
}
@Override
public void removeAttributeNode(Attribute<? super J, ?> attribute) {
attrNodeMap.remove( (PersistentAttribute<? super J, ?>) attribute );
}
@Override
public void removeAttributeNodes(Attribute.PersistentAttributeType nodeTypes) {
attrNodeMap.entrySet().removeIf( entry -> entry.getKey().getPersistentAttributeType() == nodeTypes );
}
@Override
@SuppressWarnings("unchecked")
public <AJ> AttributeNodeImplementor<AJ> findOrCreateAttributeNode(PersistentAttribute<? extends J, AJ> attribute) {
public <AJ> AttributeNodeImplementor<AJ> findOrCreateAttributeNode(PersistentAttribute<? super J, AJ> attribute) {
verifyMutability();
AttributeNodeImplementor<AJ> attrNode = null;

View File

@ -6,13 +6,16 @@
*/
package org.hibernate.graph.internal;
import org.hibernate.graph.SubGraph;
import org.hibernate.graph.spi.GraphHelper;
import org.hibernate.graph.spi.GraphImplementor;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.graph.spi.SubGraphImplementor;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.MapAttribute;
import jakarta.persistence.metamodel.PluralAttribute;
/**
* Implementation of the JPA-defined {@link jakarta.persistence.EntityGraph} interface.
*
@ -41,6 +44,11 @@ public class RootGraphImpl<J> extends AbstractGraph<J> implements RootGraphImple
return name;
}
@Override
public boolean appliesTo(EntityDomainType<?> entityType) {
return GraphHelper.appliesTo( this, entityType );
}
@Override
public RootGraphImplementor<J> makeCopy(boolean mutable) {
return new RootGraphImpl<>( null, this, mutable);
@ -57,12 +65,51 @@ public class RootGraphImpl<J> extends AbstractGraph<J> implements RootGraphImple
}
@Override
public <T1> SubGraph<? extends T1> addSubclassSubgraph(Class<? extends T1> type) {
public <S extends J> SubGraphImplementor<S> addTreatedSubgraph(Class<S> type) {
//noinspection unchecked,rawtypes
return new SubGraphImpl(this, false );
}
@Override
public <Y> SubGraphImplementor<Y> addTreatedSubgraph(Attribute<? super J, ? super Y> attribute, Class<Y> type) {
//noinspection unchecked
return (SubGraphImplementor<Y>) super.makeRootGraph( name, false );
}
@Override
public <E> SubGraphImplementor<E> addTreatedElementSubgraph(
PluralAttribute<? super J, ?, ? super E> attribute,
Class<E> type) {
throw new UnsupportedOperationException( "Not yet implemented" );
}
@Override
public <K> SubGraphImplementor<K> addTreatedMapKeySubgraph(MapAttribute<? super J, ? super K, ?> attribute, Class<K> type) {
throw new UnsupportedOperationException( "Not yet implemented" );
}
@Override
public <T1> SubGraphImplementor<? extends T1> addSubclassSubgraph(Class<? extends T1> type) {
throw new UnsupportedOperationException();
}
@Override
public boolean appliesTo(EntityDomainType<?> entityType) {
return GraphHelper.appliesTo( this, entityType );
public <E> SubGraphImplementor<E> addElementSubgraph(PluralAttribute<? super J, ?, E> attribute) {
throw new UnsupportedOperationException( "Not yet implemented" );
}
@Override
public <X> SubGraphImplementor<X> addElementSubgraph(String attributeName) {
throw new UnsupportedOperationException( "Not yet implemented" );
}
@Override
public <X> SubGraphImplementor<X> addElementSubgraph(String attributeName, Class<X> type) {
throw new UnsupportedOperationException( "Not yet implemented" );
}
@Override
public <K> SubGraphImplementor<K> addMapKeySubgraph(MapAttribute<? super J, K, ?> attribute) {
throw new UnsupportedOperationException( "Not yet implemented" );
}
}

View File

@ -9,6 +9,10 @@ package org.hibernate.graph.internal;
import org.hibernate.graph.spi.SubGraphImplementor;
import org.hibernate.metamodel.model.domain.ManagedDomainType;
import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.MapAttribute;
import jakarta.persistence.metamodel.PluralAttribute;
/**
* Implementation of the JPA-defined {@link jakarta.persistence.Subgraph} interface.
*
@ -39,4 +43,40 @@ public class SubGraphImpl<J> extends AbstractGraph<J> implements SubGraphImpleme
return super.addKeySubGraph( attributeName );
}
@Override
public <Y> SubGraphImplementor<Y> addTreatedSubgraph(Attribute<? super J, ? super Y> attribute, Class<Y> type) {
return null;
}
@Override
public <E> SubGraphImplementor<E> addTreatedElementSubgraph(
PluralAttribute<? super J, ?, ? super E> attribute,
Class<E> type) {
return null;
}
@Override
public <K> SubGraphImplementor<K> addTreatedMapKeySubgraph(MapAttribute<? super J, ? super K, ?> attribute, Class<K> type) {
throw new UnsupportedOperationException( "Not yet implemented" );
}
@Override
public <E> SubGraphImplementor<E> addElementSubgraph(PluralAttribute<? super J, ?, E> attribute) {
throw new UnsupportedOperationException( "Not yet implemented" );
}
@Override
public <X> SubGraphImplementor<X> addElementSubgraph(String attributeName) {
throw new UnsupportedOperationException( "Not yet implemented" );
}
@Override
public <X> SubGraphImplementor<X> addElementSubgraph(String attributeName, Class<X> type) {
throw new UnsupportedOperationException( "Not yet implemented" );
}
@Override
public <K> SubGraphImplementor<K> addMapKeySubgraph(MapAttribute<? super J, K, ?> attribute) {
throw new UnsupportedOperationException( "Not yet implemented" );
}
}

View File

@ -139,7 +139,7 @@ public class GraphParser extends GraphLanguageParserBaseVisitor {
final GraphImplementor<?> currentGraph = graphStack.getCurrent();
assert currentGraph != null;
final AttributeNodeImplementor attributeNode = currentGraph.addAttributeNode( attributeName );
final AttributeNodeImplementor attributeNode = currentGraph.findOrCreateAttributeNode( attributeName );
assert attributeNode != null;
return attributeNode;

View File

@ -63,14 +63,13 @@ public interface GraphImplementor<J> extends Graph<J>, GraphNodeImplementor<J> {
<AJ> AttributeNodeImplementor<AJ> findAttributeNode(String attributeName);
@Override
<AJ> AttributeNodeImplementor<AJ> findAttributeNode(PersistentAttribute<? extends J, AJ> attribute);
<AJ> AttributeNodeImplementor<AJ> findAttributeNode(PersistentAttribute<? super J, AJ> attribute);
@Override
<AJ> AttributeNodeImplementor<AJ> addAttributeNode(String attributeName)
throws CannotContainSubGraphException;
void addAttributeNode(String attributeName) throws CannotContainSubGraphException;
@Override
<AJ> AttributeNodeImplementor<AJ> addAttributeNode(PersistentAttribute<? extends J, AJ> attribute)
<AJ> AttributeNodeImplementor<AJ> addAttributeNode(PersistentAttribute<? super J, AJ> attribute)
throws CannotContainSubGraphException;
@SuppressWarnings("unchecked")
@ -80,10 +79,10 @@ public interface GraphImplementor<J> extends Graph<J>, GraphNodeImplementor<J> {
attribute = getGraphedType().findConcreteGenericAttribute( name );
}
return findOrCreateAttributeNode( (PersistentAttribute<? extends J, AJ>) attribute );
return findOrCreateAttributeNode( (PersistentAttribute<? super J, AJ>) attribute );
}
<AJ> AttributeNodeImplementor<AJ> findOrCreateAttributeNode(PersistentAttribute<? extends J, AJ> attribute);
<AJ> AttributeNodeImplementor<AJ> findOrCreateAttributeNode(PersistentAttribute<? super J, AJ> attribute);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -103,14 +102,14 @@ public interface GraphImplementor<J> extends Graph<J>, GraphNodeImplementor<J> {
}
@Override
default <AJ> SubGraphImplementor<AJ> addSubGraph(PersistentAttribute<? extends J, AJ> attribute)
default <AJ> SubGraphImplementor<AJ> addSubGraph(PersistentAttribute<? super J, AJ> attribute)
throws CannotContainSubGraphException {
return findOrCreateAttributeNode( attribute ).makeSubGraph();
}
@Override
default <AJ> SubGraphImplementor<? extends AJ> addSubGraph(
PersistentAttribute<? extends J, AJ> attribute,
PersistentAttribute<? super J, AJ> attribute,
Class<? extends AJ> subType) throws CannotContainSubGraphException {
return findOrCreateAttributeNode( attribute ).makeSubGraph( subType );
}
@ -131,13 +130,13 @@ public interface GraphImplementor<J> extends Graph<J>, GraphNodeImplementor<J> {
}
@Override
default <AJ> SubGraphImplementor<AJ> addKeySubGraph(PersistentAttribute<? extends J, AJ> attribute) {
default <AJ> SubGraphImplementor<AJ> addKeySubGraph(PersistentAttribute<? super J, AJ> attribute) {
return findOrCreateAttributeNode( attribute ).makeKeySubGraph();
}
@Override
default <AJ> SubGraphImplementor<? extends AJ> addKeySubGraph(
PersistentAttribute<? extends J, AJ> attribute,
PersistentAttribute<? super J, AJ> attribute,
Class<? extends AJ> subType)
throws CannotContainSubGraphException {
return findOrCreateAttributeNode( attribute ).makeKeySubGraph( subType );

View File

@ -36,10 +36,10 @@ public interface SubGraphImplementor<J> extends SubGraph<J>, GraphImplementor<J>
<AJ> SubGraphImplementor<AJ> addKeySubGraph(String attributeName);
@Override
<AJ> AttributeNodeImplementor<AJ> addAttributeNode(PersistentAttribute<? extends J, AJ> attribute);
<AJ> AttributeNodeImplementor<AJ> addAttributeNode(PersistentAttribute<? super J, AJ> attribute);
@Override
default <AJ> SubGraphImplementor<? extends AJ> addKeySubGraph(PersistentAttribute<? extends J, AJ> attribute, Class<? extends AJ> subType)
default <AJ> SubGraphImplementor<? extends AJ> addKeySubGraph(PersistentAttribute<? super J, AJ> attribute, Class<? extends AJ> subType)
throws CannotContainSubGraphException {
return null;
}

View File

@ -460,7 +460,7 @@ public class JpaMetamodelImpl implements JpaMetamodelImplementor, Serializable {
GraphImplementor<?> graphNode) {
for ( NamedAttributeNode namedAttributeNode : namedAttributeNodes ) {
final String value = namedAttributeNode.value();
AttributeNodeImplementor<?> attributeNode = graphNode.addAttributeNode( value );
AttributeNodeImplementor<?> attributeNode = graphNode.findOrCreateAttributeNode( value );
if ( StringHelper.isNotEmpty( namedAttributeNode.subgraph() ) ) {
final SubGraphImplementor<?> subgraph = attributeNode.makeSubGraph();
applyNamedSubgraphs(

View File

@ -17,7 +17,9 @@ earlier versions, see any other pertinent migration guides as well.
* type parameters
* Affects much of the Criteria API - especially roots, joins, paths
* Affects much of the Graph API
* Affects much of the Graph API -
* org.hibernate.graph.Graph.addAttributeNode(java.lang.String) defines a return while
1jakarta.persistence.Graph.addAttributeNode(java.lang.String)` does not.
* new JPA features colliding with previous Hibernate extension features
* `Nulls` (JPA) v. `NullPrecedence` (Hibernate), including JPA's new `Order#getNullPrecedence()` returning `Nulls`
colliding with Hibernate's `SqmSortSpecification#getNullPrecedence` returning `NullPrecedence`. Hibernate's form