org.hibernate.jpa.graph.internal.EntityGraphImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
Hibernate's core ORM functionality
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.jpa.graph.internal;
import java.util.List;
import javax.persistence.AttributeNode;
import javax.persistence.EntityGraph;
import javax.persistence.Subgraph;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.IdentifiableType;
import javax.persistence.metamodel.ManagedType;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.graph.spi.EntityGraphImplementor;
/**
* The Hibernate implementation of the JPA EntityGraph contract.
*
* @author Steve Ebersole
*/
public class EntityGraphImpl extends AbstractGraphNode implements EntityGraph, EntityGraphImplementor {
private final String name;
private final EntityType entityType;
public EntityGraphImpl(String name, EntityType entityType, SessionFactoryImplementor sessionFactory) {
super( sessionFactory, true );
this.name = name;
this.entityType = entityType;
}
public EntityGraphImpl makeImmutableCopy(String name) {
return new EntityGraphImpl<>( name, this, false );
}
public EntityGraphImpl makeMutableCopy() {
return new EntityGraphImpl<>( name, this, true );
}
private EntityGraphImpl(String name, EntityGraphImpl original, boolean mutable) {
super( original, mutable );
this.name = name;
this.entityType = original.entityType;
}
public EntityType getEntityType() {
return entityType;
}
@Override
public String getName() {
return name;
}
@Override
public void addAttributeNodes(String... attributeNames) {
super.addAttributeNodes( attributeNames );
}
@Override
@SafeVarargs
public final void addAttributeNodes(Attribute... attributes) {
super.addAttributeNodes( attributes );
}
@Override
public SubgraphImpl addSubgraph(Attribute attribute) {
return super.addSubgraph( attribute );
}
@Override
public SubgraphImpl addSubgraph(Attribute attribute, Class type) {
return super.addSubgraph( attribute, type );
}
@Override
public SubgraphImpl addSubgraph(String attributeName) {
return super.addSubgraph( attributeName );
}
@Override
public SubgraphImpl addSubgraph(String attributeName, Class type) {
return super.addSubgraph( attributeName, type );
}
@Override
public SubgraphImpl addKeySubgraph(Attribute attribute) {
return super.addKeySubgraph( attribute );
}
@Override
public SubgraphImpl addKeySubgraph(Attribute attribute, Class type) {
return super.addKeySubgraph( attribute, type );
}
@Override
public SubgraphImpl addKeySubgraph(String attributeName) {
return super.addKeySubgraph( attributeName );
}
@Override
public SubgraphImpl addKeySubgraph(String attributeName, Class type) {
return super.addKeySubgraph( attributeName, type );
}
@Override
public Subgraph addSubclassSubgraph(Class type) {
// todo : implement
throw new NotYetImplementedException();
}
@Override
public List> getAttributeNodes() {
return super.attributeNodes();
}
@Override
@SuppressWarnings("unchecked")
protected Attribute resolveAttribute(String attributeName) {
final Attribute attribute = entityType.getAttribute( attributeName );
if ( attribute == null ) {
throw new IllegalArgumentException(
String.format(
"Given attribute name [%s] is not an attribute on this entity [%s]",
attributeName,
entityType.getName()
)
);
}
return attribute;
}
@SuppressWarnings("unchecked")
public boolean appliesTo(String entityName) {
return appliesTo( getFactory().getMetamodel().entity( entityName ) );
}
public boolean appliesTo(EntityType entityType) {
if ( this.entityType.equals( entityType ) ) {
return true;
}
IdentifiableType superType = entityType.getSupertype();
while ( superType != null ) {
if ( superType.equals( entityType ) ) {
return true;
}
superType = superType.getSupertype();
}
return false;
}
@Override
ManagedType getManagedType() {
return entityType;
}
}