Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.hibernate.boot.model.NamedEntityGraphDefinition;
import org.hibernate.boot.model.TypeDefinition;
import org.hibernate.boot.model.TypeDefinitionRegistry;
import org.hibernate.boot.model.TypeDefinitionRegistryStandardImpl;
import org.hibernate.boot.model.convert.internal.AttributeConverterManager;
import org.hibernate.boot.model.convert.internal.ClassBasedConverterDescriptor;
import org.hibernate.boot.model.convert.spi.ConverterAutoApplyHandler;
Expand Down Expand Up @@ -710,7 +709,7 @@ public Map<String, NamedEntityGraphDefinition> getNamedEntityGraphs() {

@Override
public void addNamedEntityGraph(NamedEntityGraphDefinition definition) {
final String name = definition.getRegisteredName();
final String name = definition.name();
final NamedEntityGraphDefinition previous = namedEntityGraphMap.put( name, definition );
if ( previous != null ) {
throw new DuplicateMappingException( DuplicateMappingException.Type.NAMED_ENTITY_GRAPH, name );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package org.hibernate.boot.internal;

import org.hibernate.boot.model.TypeDefinitionRegistryStandardImpl;
import org.hibernate.boot.model.naming.ObjectNameNormalizer;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.InFlightMetadataCollector;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.boot.internal;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.hibernate.boot.model.TypeDefinition;
import org.hibernate.boot.model.TypeDefinitionRegistry;
import org.hibernate.type.descriptor.java.BasicJavaType;

import org.jboss.logging.Logger;

import static org.hibernate.internal.util.StringHelper.isEmpty;

/**
* Basic implementation of {@link TypeDefinitionRegistry}.
*
* @author Chris Cranford
*/
public class TypeDefinitionRegistryStandardImpl implements TypeDefinitionRegistry {
private static final Logger log = Logger.getLogger( TypeDefinitionRegistryStandardImpl.class );

private final TypeDefinitionRegistry parent;
private final Map<String, TypeDefinition> typeDefinitionMap = new HashMap<>();

public TypeDefinitionRegistryStandardImpl() {
this( null );
}

public TypeDefinitionRegistryStandardImpl(TypeDefinitionRegistry parent) {
this.parent = parent;
}

@Override
public TypeDefinition resolve(String typeName) {
final TypeDefinition localDefinition = typeDefinitionMap.get( typeName );
if ( localDefinition != null ) {
return localDefinition;
}
else if ( parent != null ) {
return parent.resolve( typeName );
}
else {
return null;
}
}

@Override
public TypeDefinition resolveAutoApplied(BasicJavaType<?> jtd) {
// For now, check the definition map for an entry keyed by the JTD name.
// Ultimately should maybe have TypeDefinition or the registry keep explicit
// track of auto-applied definitions.
return jtd.getJavaType() == null ? null : typeDefinitionMap.get( jtd.getTypeName() );
}

@Override
public TypeDefinitionRegistry register(TypeDefinition typeDefinition) {
return register( typeDefinition, DuplicationStrategy.OVERWRITE );
}

@Override
public TypeDefinitionRegistry register(TypeDefinition typeDefinition, DuplicationStrategy duplicationStrategy) {
if ( typeDefinition == null ) {
throw new IllegalArgumentException( "TypeDefinition to register cannot be null" );
}

if ( typeDefinition.getTypeImplementorClass() == null ) {
throw new IllegalArgumentException( "TypeDefinition to register cannot define null #typeImplementorClass" );
}

if ( !isEmpty( typeDefinition.getName() ) ) {
register( typeDefinition.getName(), typeDefinition, duplicationStrategy );
}

if ( typeDefinition.getRegistrationKeys() != null ) {
for ( String registrationKey : typeDefinition.getRegistrationKeys() ) {
register( registrationKey, typeDefinition, duplicationStrategy );
}
}

return this;
}

private void register(String name, TypeDefinition typeDefinition, DuplicationStrategy duplicationStrategy) {
if ( duplicationStrategy == DuplicationStrategy.KEEP ) {
if ( !typeDefinitionMap.containsKey( name ) ) {
typeDefinitionMap.put( name, typeDefinition );
}
}
else {
final TypeDefinition existing = typeDefinitionMap.put( name, typeDefinition );
if ( existing != null && existing != typeDefinition ) {
if ( duplicationStrategy == DuplicationStrategy.OVERWRITE ) {
log.debugf( "Overwrote existing registration [%s] for type definition.", name );
}
else {
throw new IllegalArgumentException(
String.format(
Locale.ROOT,
"Attempted to overwrite registration [%s] for type definition.",
name
)
);
}
}
}
}

@Override
public Map<String, TypeDefinition> copyRegistrationMap() {
return new HashMap<>( typeDefinitionMap );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,74 +5,29 @@
package org.hibernate.boot.model;

import jakarta.persistence.NamedEntityGraph;
import org.hibernate.mapping.PersistentClass;

import static org.hibernate.internal.util.StringHelper.isNotEmpty;
import java.util.Objects;

/**
* Models a {@linkplain NamedEntityGraph @NamedEntityGraph}
*
* @author Steve Ebersole
*/
public class NamedEntityGraphDefinition {
public record NamedEntityGraphDefinition
(String name, String entityName, Source source, NamedGraphCreator graphCreator) {
public enum Source { JPA, PARSED }

private final String name;

private final String entityName;

private final Source source;
private final NamedGraphCreator graphCreator;

public NamedEntityGraphDefinition(jakarta.persistence.NamedEntityGraph annotation, String jpaEntityName, String entityName) {
this.name = isNotEmpty( annotation.name() ) ? annotation.name() : jpaEntityName;
if ( name == null ) {
throw new IllegalArgumentException( "Named entity graph name cannot be null" );
}

this.entityName = entityName;

source = Source.JPA;
graphCreator = new NamedGraphCreatorJpa( annotation, jpaEntityName );
}

public NamedEntityGraphDefinition(org.hibernate.annotations.NamedEntityGraph annotation, PersistentClass persistentClass) {
this.name = isNotEmpty( annotation.name() ) ? annotation.name() : persistentClass.getJpaEntityName();
if ( name == null ) {
throw new IllegalArgumentException( "Named entity graph name cannot be null" );
}

this.entityName = persistentClass.getEntityName();

source = Source.PARSED;
graphCreator = new NamedGraphCreatorParsed( persistentClass.getMappedClass(), annotation );
}

public NamedEntityGraphDefinition(org.hibernate.annotations.NamedEntityGraph annotation) {
this.name = annotation.name();
if ( name == null ) {
throw new IllegalArgumentException( "Named entity graph name cannot be null" );
}

this.entityName = null;

source = Source.PARSED;
graphCreator = new NamedGraphCreatorParsed( annotation );
public NamedEntityGraphDefinition {
Objects.requireNonNull( name, "Named entity graph name cannot be null" );
}

@Deprecated(since = "7.0", forRemoval = true)
public String getRegisteredName() {
return name;
}

@Deprecated(since = "7.0", forRemoval = true)
public String getEntityName() {
return entityName;
}

public Source getSource() {
return source;
}

public NamedGraphCreator getGraphCreator() {
return graphCreator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,112 +4,23 @@
*/
package org.hibernate.boot.model;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.hibernate.type.descriptor.java.BasicJavaType;

import org.jboss.logging.Logger;

import static org.hibernate.internal.util.StringHelper.isEmpty;

/**
* Basic implementation of {@link TypeDefinitionRegistry}.
*
* @author Chris Cranford
* @deprecated Internal code should use the internal implementation class
* {@link org.hibernate.boot.internal.TypeDefinitionRegistryStandardImpl}.
* This class will be removed.
*/
public class TypeDefinitionRegistryStandardImpl implements TypeDefinitionRegistry {
private static final Logger log = Logger.getLogger( TypeDefinitionRegistryStandardImpl.class );

private final TypeDefinitionRegistry parent;
private final Map<String, TypeDefinition> typeDefinitionMap = new HashMap<>();
@Deprecated(since = "7.0", forRemoval = true)
public class TypeDefinitionRegistryStandardImpl
extends org.hibernate.boot.internal.TypeDefinitionRegistryStandardImpl {

public TypeDefinitionRegistryStandardImpl() {
this( null );
super();
}

public TypeDefinitionRegistryStandardImpl(TypeDefinitionRegistry parent) {
this.parent = parent;
}

@Override
public TypeDefinition resolve(String typeName) {
final TypeDefinition localDefinition = typeDefinitionMap.get( typeName );
if ( localDefinition != null ) {
return localDefinition;
}
else if ( parent != null ) {
return parent.resolve( typeName );
}
else {
return null;
}
}

@Override
public TypeDefinition resolveAutoApplied(BasicJavaType<?> jtd) {
// For now, check the definition map for an entry keyed by the JTD name.
// Ultimately should maybe have TypeDefinition or the registry keep explicit
// track of auto-applied definitions.
return jtd.getJavaType() == null ? null : typeDefinitionMap.get( jtd.getTypeName() );
}

@Override
public TypeDefinitionRegistry register(TypeDefinition typeDefinition) {
return register( typeDefinition, DuplicationStrategy.OVERWRITE );
}

@Override
public TypeDefinitionRegistry register(TypeDefinition typeDefinition, DuplicationStrategy duplicationStrategy) {
if ( typeDefinition == null ) {
throw new IllegalArgumentException( "TypeDefinition to register cannot be null" );
}

if ( typeDefinition.getTypeImplementorClass() == null ) {
throw new IllegalArgumentException( "TypeDefinition to register cannot define null #typeImplementorClass" );
}

if ( !isEmpty( typeDefinition.getName() ) ) {
register( typeDefinition.getName(), typeDefinition, duplicationStrategy );
}

if ( typeDefinition.getRegistrationKeys() != null ) {
for ( String registrationKey : typeDefinition.getRegistrationKeys() ) {
register( registrationKey, typeDefinition, duplicationStrategy );
}
}

return this;
super(parent);
}

private void register(String name, TypeDefinition typeDefinition, DuplicationStrategy duplicationStrategy) {
if ( duplicationStrategy == DuplicationStrategy.KEEP ) {
if ( !typeDefinitionMap.containsKey( name ) ) {
typeDefinitionMap.put( name, typeDefinition );
}
}
else {
final TypeDefinition existing = typeDefinitionMap.put( name, typeDefinition );
if ( existing != null && existing != typeDefinition ) {
if ( duplicationStrategy == DuplicationStrategy.OVERWRITE ) {
log.debugf( "Overwrote existing registration [%s] for type definition.", name );
}
else {
throw new IllegalArgumentException(
String.format(
Locale.ROOT,
"Attempted to overwrite registration [%s] for type definition.",
name
)
);
}
}
}
}

@Override
public Map<String, TypeDefinition> copyRegistrationMap() {
return new HashMap<>( typeDefinitionMap );
}
}
Loading