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 @@ -14,7 +14,6 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import jakarta.validation.NoProviderFoundException;
import jakarta.validation.constraints.Digits;
Expand All @@ -25,7 +24,6 @@
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import org.hibernate.AssertionFailure;
import org.hibernate.MappingException;
import org.hibernate.boot.internal.ClassLoaderAccessImpl;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
Expand Down Expand Up @@ -60,10 +58,10 @@
import static java.util.Collections.disjoint;
import static org.hibernate.boot.beanvalidation.BeanValidationIntegrator.APPLY_CONSTRAINTS;
import static org.hibernate.boot.beanvalidation.GroupsPerOperation.buildGroupsForOperation;
import static org.hibernate.boot.model.internal.BinderHelper.findPropertyByName;
import static org.hibernate.cfg.ValidationSettings.CHECK_NULLABILITY;
import static org.hibernate.cfg.ValidationSettings.JAKARTA_VALIDATION_FACTORY;
import static org.hibernate.cfg.ValidationSettings.JPA_VALIDATION_FACTORY;
import static org.hibernate.internal.util.StringHelper.isEmpty;
import static org.hibernate.internal.util.StringHelper.isNotEmpty;

/**
Expand Down Expand Up @@ -496,72 +494,6 @@ private static boolean isValidatorLengthAnnotation(ConstraintDescriptor<?> descr
.equals( descriptor.getAnnotation().annotationType().getName() );
}

/**
* Locate the property by path in a recursive way, including IdentifierProperty in the loop if propertyName is
* {@code null}. If propertyName is {@code null} or empty, the IdentifierProperty is returned
*/
private static Property findPropertyByName(PersistentClass associatedClass, String propertyName) {
Property property = null;
final Property idProperty = associatedClass.getIdentifierProperty();
final String idName = idProperty != null ? idProperty.getName() : null;
try {
if ( isEmpty( propertyName ) || propertyName.equals( idName ) ) {
//default to id
property = idProperty;
}
else {
if ( propertyName.indexOf( idName + "." ) == 0 ) {
property = idProperty;
propertyName = propertyName.substring( idName.length() + 1 );
}
final StringTokenizer tokens = new StringTokenizer( propertyName, ".", false );
while ( tokens.hasMoreTokens() ) {
final String element = tokens.nextToken();
if ( property == null ) {
property = associatedClass.getProperty( element );
}
else {
if ( property.isComposite() ) {
property = ( (Component) property.getValue() ).getProperty( element );
}
else {
return null;
}
}
}
}
}
catch ( MappingException e ) {
try {
//if we do not find it, try to check the identifier mapper
if ( associatedClass.getIdentifierMapper() == null ) {
return null;
}
else {
final StringTokenizer tokens = new StringTokenizer( propertyName, ".", false );
while ( tokens.hasMoreTokens() ) {
final String element = tokens.nextToken();
if ( property == null ) {
property = associatedClass.getIdentifierMapper().getProperty( element );
}
else {
if ( property.isComposite() ) {
property = ( (Component) property.getValue() ).getProperty( element );
}
else {
return null;
}
}
}
}
}
catch ( MappingException ee ) {
return null;
}
}
return property;
}

private static ValidatorFactory getValidatorFactory(ActivationContext context) {
// IMPL NOTE: We can either be provided a ValidatorFactory or make one. We can be provided
// a ValidatorFactory in 2 different ways. So here we "get" a ValidatorFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,9 @@ Property resolveMapsId() {
final PersistentClass persistentClass = getPropertyHolder().getPersistentClass();
final KeyValue identifier = persistentClass.getIdentifier();
try {
if ( identifier instanceof Component embeddedIdType ) {
// an @EmbeddedId
return embeddedIdType.getProperty( getMapsId() );
}
else {
// a simple id or an @IdClass
return persistentClass.getProperty( getMapsId() );
}
return identifier instanceof Component embeddedIdType
? embeddedIdType.getProperty( getMapsId() ) // an @EmbeddedId
: persistentClass.getProperty( getMapsId() ); // a simple id or an @IdClass
}
catch (MappingException me) {
throw new AnnotationException( "Identifier field '" + getMapsId()
Expand All @@ -255,10 +250,10 @@ public List<AnnotatedJoinColumn> getJoinColumns() {

@Override
public void addColumn(AnnotatedColumn child) {
if ( !( child instanceof AnnotatedJoinColumn ) ) {
if ( !( child instanceof AnnotatedJoinColumn joinColumn ) ) {
throw new AssertionFailure( "wrong sort of column" );
}
addColumn( (AnnotatedJoinColumn) child );
addColumn( joinColumn );
}

public void addColumn(AnnotatedJoinColumn child) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,114 +583,84 @@ private static void matchColumnsByProperty(Property property, Map<Column, Set<Pr
* If propertyName is null or empty, the IdentifierProperty is returned
*/
public static Property findPropertyByName(PersistentClass associatedClass, String propertyName) {
Property property = null;
final Property idProperty = associatedClass.getIdentifierProperty();
final String idName = idProperty == null ? null : idProperty.getName();
try {
if ( isEmpty( propertyName ) || propertyName.equals( idName ) ) {
//default to id
property = idProperty;
}
else {
if ( propertyName.indexOf( idName + "." ) == 0 ) {
property = idProperty;
propertyName = propertyName.substring( idName.length() + 1 );
}
final var tokens = new StringTokenizer( propertyName, ".", false );
while ( tokens.hasMoreTokens() ) {
final String element = tokens.nextToken();
if ( property == null ) {
property = associatedClass.getProperty( element );
}
else {
if ( !property.isComposite() ) {
return null;
}
property = ( (Component) property.getValue() ).getProperty( element );
}
}
}
return isEmpty( propertyName ) || propertyName.equals( idName )
? idProperty // Default to id
: findProperty( associatedClass, propertyName, idProperty, idName );
}
catch ( MappingException e ) {
try {
// if we do not find it, try to check the identifier mapper
if ( associatedClass.getIdentifierMapper() == null ) {
return null;
}
final var tokens = new StringTokenizer( propertyName, ".", false );
while ( tokens.hasMoreTokens() ) {
final String element = tokens.nextToken();
if ( property == null ) {
property = associatedClass.getIdentifierMapper().getProperty( element );
}
else {
if ( !property.isComposite() ) {
return null;
}
property = ( (Component) property.getValue() ).getProperty( element );
}
}
return findPropertyUsingIdMapper( associatedClass, propertyName );
}
catch ( MappingException ee ) {
return null;
}
}
return property;
}

/**
* Retrieve the property by path in a recursive way
*/
public static Property findPropertyByName(Component component, String propertyName) {
Property property = null;
try {
if ( isEmpty( propertyName ) ) {
// Do not expect to use a primary key for this case
return null;
}
else {
final var tokens = new StringTokenizer( propertyName, ".", false );
while ( tokens.hasMoreTokens() ) {
final String element = tokens.nextToken();
if ( property == null ) {
property = component.getProperty( element );
}
else {
if ( !property.isComposite() ) {
return null;
}
property = ( (Component) property.getValue() ).getProperty( element );
}
}
}
return isEmpty( propertyName )
? null // Do not expect to use a primary key for this case
: findProperty( component, propertyName, null );
}
catch (MappingException e) {
try {
// if we do not find it, try to check the identifier mapper
if ( component.getOwner().getIdentifierMapper() == null ) {
return null;
}
final var tokens = new StringTokenizer( propertyName, ".", false );
while ( tokens.hasMoreTokens() ) {
final String element = tokens.nextToken();
if ( property == null ) {
property = component.getOwner().getIdentifierMapper().getProperty( element );
}
else {
if ( !property.isComposite() ) {
return null;
}
property = ( (Component) property.getValue() ).getProperty( element );
}
}
return findPropertyUsingIdMapper( component.getOwner(), propertyName );
}
catch (MappingException ee) {
return null;
}
}
}

private static Property findProperty(
PersistentClass associatedClass, String propertyName,
Property idProperty, String idName) {
Property property;
// Handle id property
final String name;
if ( propertyName.indexOf( idName + "." ) == 0 ) {
property = idProperty;
name = propertyName.substring( idName.length() + 1 );
}
else {
property = null;
name = propertyName;
}
return findProperty( associatedClass, name, property );
}

private static Property findProperty(AttributeContainer root, String name, Property property) {
final var tokens = new StringTokenizer( name, ".", false );
while ( tokens.hasMoreTokens() ) {
final String element = tokens.nextToken();
if ( property == null ) {
property = root.getProperty( element );
}
else if ( property.isComposite() ) {
final var value = (Component) property.getValue();
property = value.getProperty( element );
}
else {
return null;
}
}
return property;
}

private static Property findPropertyUsingIdMapper(PersistentClass associatedClass, String propertyName) {
final var identifierMapper = associatedClass.getIdentifierMapper();
return identifierMapper == null ? null : findProperty( identifierMapper, propertyName, null );
}

public static String getRelativePath(PropertyHolder propertyHolder, String propertyName) {
if ( propertyHolder == null ) {
return propertyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,8 @@ else if ( value instanceof Component component ) {
value = component.getProperty( element ).getValue();
}
else if ( value instanceof ToOne toOne ) {
value = collector
.getEntityBinding( toOne.getReferencedEntityName() )
.getProperty( element )
.getValue();
final var entity = collector.getEntityBinding( toOne.getReferencedEntityName() );
value = entity.getProperty( element ).getValue();
}
else if ( value instanceof OneToMany oneToMany ) {
value = oneToMany.getAssociatedClass().getProperty( element ).getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package org.hibernate.mapping;

import org.hibernate.MappingException;

/**
* Identifies a mapping model object which may have {@linkplain Property attributes}
* (fields or properties). Abstracts over {@link PersistentClass} and {@link Join}.
Expand All @@ -22,9 +24,26 @@
*/
public interface AttributeContainer {
/**
* Add a property to this {@link PersistentClass} or {@link Join}.
* Add an attribute to this {@link PersistentClass} or {@link Join}.
*/
void addProperty(Property property);

/**
* Determine if the given attribute belongs to this container.
*/
boolean contains(Property property);

/**
* Get the attribute with the given name belonging to this container.
* @param propertyName the name of an attribute
* @throws MappingException if there is no attribute with the given name
* @since 7.2
*/
Property getProperty(String propertyName) throws MappingException;

/**
* The {@link Table} with the columns mapped by attributes belonging
* to this container.
*/
Table getTable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
* @author Gavin King
* @author Steve Ebersole
*/
public class Component extends SimpleValue implements MetaAttributable, SortableValue {
public class Component extends SimpleValue implements AttributeContainer, MetaAttributable, SortableValue {

private String componentClassName;
private boolean embedded;
Expand Down Expand Up @@ -173,6 +173,7 @@ public void addProperty(Property p, ClassDetails declaringClass) {
}
}

@Override
public void addProperty(Property p) {
addProperty( p, null );
}
Expand Down Expand Up @@ -393,6 +394,11 @@ private CompositeUserType<?> createCompositeUserType(Component component) {
: FallbackBeanInstanceProducer.INSTANCE.produceBeanInstance( clazz );
}

@Override
public boolean contains(Property property) {
return properties.contains( property );
}

@Override
public CompositeType getType() throws MappingException {
// Resolve the type of the value once and for all as this operation generates a proxy class
Expand Down Expand Up @@ -546,6 +552,7 @@ public Property getProperty(int index) {
return properties.get( index );
}

@Override
public Property getProperty(String propertyName) throws MappingException {
for ( Property prop : properties ) {
if ( prop.getName().equals(propertyName) ) {
Expand Down
Loading