Skip to content

Commit

Permalink
HHH-17377 - Migrate to JPA 3.2
Browse files Browse the repository at this point in the history
https://hibernate.atlassian.net/browse/HHH-17377

Just Graph completed - 2 left related to Session/EntityManager
  • Loading branch information
sebersole committed Nov 17, 2023
1 parent b7d4db4 commit e288be8
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 70 deletions.
35 changes: 21 additions & 14 deletions hibernate-core/src/main/java/org/hibernate/graph/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

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;
Expand All @@ -29,7 +32,7 @@
* @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
Expand All @@ -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() );
}

Expand Down Expand Up @@ -92,24 +95,18 @@ RootGraph<J> makeRootGraph(String name, boolean mutable)
* 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);



Expand All @@ -134,10 +131,10 @@ <AJ> SubGraph<AJ> addSubGraph(String attributeName, Class<AJ> type)
*
* @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;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -148,8 +145,18 @@ <AJ> SubGraph<AJ> addKeySubGraph(String attributeName)
<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);
}
26 changes: 8 additions & 18 deletions hibernate-core/src/main/java/org/hibernate/graph/RootGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,13 @@ default void addAttributeNodes(String... names) {
}

@Override
@SuppressWarnings("unchecked")
default void addAttributeNodes(Attribute<J, ?>... attributes) {
if ( attributes != null ) {
for ( Attribute<J, ?> attribute : attributes ) {
addAttributeNode( (PersistentAttribute<J,?>) attribute );
}
}
}

@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) {
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
Expand All @@ -78,13 +68,13 @@ default <X> SubGraph<X> addSubgraph(String name, Class<X> type) {
}

@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
Expand Down
21 changes: 10 additions & 11 deletions hibernate-core/src/main/java/org/hibernate/graph/SubGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,24 @@ default void addAttributeNodes(String... names) {
}

@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 );
}

Expand All @@ -75,13 +74,13 @@ default <X> SubGraph<X> addSubgraph(String name, Class<X> type) {


@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.hibernate.metamodel.model.domain.ManagedDomainType;
import org.hibernate.metamodel.model.domain.PersistentAttribute;

import jakarta.persistence.metamodel.Attribute;

import static java.util.Collections.emptyList;

/**
Expand Down Expand Up @@ -76,7 +78,7 @@ public void merge(GraphImplementor<? extends J> other) {

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
Expand Down Expand Up @@ -124,12 +126,12 @@ public AttributeNodeImplementor<?> addAttributeNode(AttributeNodeImplementor<?>
@SuppressWarnings("unchecked")
public <AJ> AttributeNodeImplementor<AJ> findAttributeNode(String attributeName) {
final PersistentAttribute<? super J, ?> attribute = managedType.findAttributeInSuperTypes( 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 );
}

Expand All @@ -145,20 +147,54 @@ public List<AttributeNodeImplementor<?>> getAttributeNodeImplementors() {
}

@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -41,6 +44,11 @@ public String getName() {
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);
Expand All @@ -57,12 +65,51 @@ public RootGraphImplementor<J> makeRootGraph(String name, boolean mutable) {
}

@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" );
}
}

0 comments on commit e288be8

Please sign in to comment.