Skip to content

Commit 2328bd9

Browse files
committed
simplify logic in BinderHelper by making Component and AttributeContainer
and by refactoring
1 parent bc5370a commit 2328bd9

File tree

7 files changed

+88
-93
lines changed

7 files changed

+88
-93
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java

Lines changed: 48 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -586,57 +586,14 @@ public static Property findPropertyByName(PersistentClass associatedClass, Strin
586586
final Property idProperty = associatedClass.getIdentifierProperty();
587587
final String idName = idProperty == null ? null : idProperty.getName();
588588
try {
589-
if ( isEmpty( propertyName ) || propertyName.equals( idName ) ) {
590-
//default to id
591-
return idProperty;
592-
}
593-
else {
594-
Property property = null;
595-
if ( propertyName.indexOf( idName + "." ) == 0 ) {
596-
property = idProperty;
597-
propertyName = propertyName.substring( idName.length() + 1 );
598-
}
599-
final var tokens = new StringTokenizer( propertyName, ".", false );
600-
while ( tokens.hasMoreTokens() ) {
601-
final String element = tokens.nextToken();
602-
if ( property == null ) {
603-
property = associatedClass.getProperty( element );
604-
}
605-
else if ( property.isComposite() ) {
606-
final var value = (Component) property.getValue();
607-
property = value.getProperty( element );
608-
}
609-
else {
610-
return null;
611-
}
612-
}
613-
return property;
614-
}
589+
return isEmpty( propertyName ) || propertyName.equals( idName )
590+
? idProperty // Default to id
591+
: findProperty( associatedClass, propertyName, idProperty, idName );
615592
}
616593
catch ( MappingException e ) {
617594
try {
618595
// if we do not find it, try to check the identifier mapper
619-
if ( associatedClass.getIdentifierMapper() == null ) {
620-
return null;
621-
}
622-
else {
623-
Property property = null;
624-
final var tokens = new StringTokenizer( propertyName, ".", false );
625-
while ( tokens.hasMoreTokens() ) {
626-
final String element = tokens.nextToken();
627-
if ( property == null ) {
628-
property = associatedClass.getIdentifierMapper().getProperty( element );
629-
}
630-
else if ( property.isComposite() ) {
631-
final var value = (Component) property.getValue();
632-
property = value.getProperty( element );
633-
}
634-
else {
635-
return null;
636-
}
637-
}
638-
return property;
639-
}
596+
return findPropertyUsingIdMapper( associatedClass, propertyName );
640597
}
641598
catch ( MappingException ee ) {
642599
return null;
@@ -649,60 +606,61 @@ else if ( property.isComposite() ) {
649606
*/
650607
public static Property findPropertyByName(Component component, String propertyName) {
651608
try {
652-
if ( isEmpty( propertyName ) ) {
653-
// Do not expect to use a primary key for this case
654-
return null;
655-
}
656-
else {
657-
Property property = null;
658-
final var tokens = new StringTokenizer( propertyName, ".", false );
659-
while ( tokens.hasMoreTokens() ) {
660-
final String element = tokens.nextToken();
661-
if ( property == null ) {
662-
property = component.getProperty( element );
663-
}
664-
else if ( property.isComposite() ) {
665-
final var value = (Component) property.getValue();
666-
property = value.getProperty( element );
667-
}
668-
else {
669-
return null;
670-
}
671-
}
672-
return property;
673-
}
609+
return isEmpty( propertyName )
610+
? null // Do not expect to use a primary key for this case
611+
: findProperty( component, propertyName, null );
674612
}
675613
catch (MappingException e) {
676614
try {
677615
// if we do not find it, try to check the identifier mapper
678-
if ( component.getOwner().getIdentifierMapper() == null ) {
679-
return null;
680-
}
681-
else {
682-
Property property = null;
683-
final var tokens = new StringTokenizer( propertyName, ".", false );
684-
while ( tokens.hasMoreTokens() ) {
685-
final String element = tokens.nextToken();
686-
if ( property == null ) {
687-
property = component.getOwner().getIdentifierMapper().getProperty( element );
688-
}
689-
else if ( property.isComposite() ) {
690-
final var value = (Component) property.getValue();
691-
property = value.getProperty( element );
692-
}
693-
else {
694-
return null;
695-
}
696-
}
697-
return property;
698-
}
616+
return findPropertyUsingIdMapper( component.getOwner(), propertyName );
699617
}
700618
catch (MappingException ee) {
701619
return null;
702620
}
703621
}
704622
}
705623

624+
private static Property findProperty(
625+
PersistentClass associatedClass, String propertyName,
626+
Property idProperty, String idName) {
627+
Property property;
628+
// Handle id property
629+
final String name;
630+
if ( propertyName.indexOf( idName + "." ) == 0 ) {
631+
property = idProperty;
632+
name = propertyName.substring( idName.length() + 1 );
633+
}
634+
else {
635+
property = null;
636+
name = propertyName;
637+
}
638+
return findProperty( associatedClass, name, property );
639+
}
640+
641+
private static Property findProperty(AttributeContainer root, String name, Property property) {
642+
final var tokens = new StringTokenizer( name, ".", false );
643+
while ( tokens.hasMoreTokens() ) {
644+
final String element = tokens.nextToken();
645+
if ( property == null ) {
646+
property = root.getProperty( element );
647+
}
648+
else if ( property.isComposite() ) {
649+
final var value = (Component) property.getValue();
650+
property = value.getProperty( element );
651+
}
652+
else {
653+
return null;
654+
}
655+
}
656+
return property;
657+
}
658+
659+
private static Property findPropertyUsingIdMapper(PersistentClass associatedClass, String propertyName) {
660+
final var identifierMapper = associatedClass.getIdentifierMapper();
661+
return identifierMapper == null ? null : findProperty( identifierMapper, propertyName, null );
662+
}
663+
706664
public static String getRelativePath(PropertyHolder propertyHolder, String propertyName) {
707665
if ( propertyHolder == null ) {
708666
return propertyName;

hibernate-core/src/main/java/org/hibernate/mapping/AttributeContainer.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
package org.hibernate.mapping;
66

7+
import org.hibernate.MappingException;
8+
79
/**
810
* Identifies a mapping model object which may have {@linkplain Property attributes}
911
* (fields or properties). Abstracts over {@link PersistentClass} and {@link Join}.
@@ -22,9 +24,26 @@
2224
*/
2325
public interface AttributeContainer {
2426
/**
25-
* Add a property to this {@link PersistentClass} or {@link Join}.
27+
* Add an attribute to this {@link PersistentClass} or {@link Join}.
2628
*/
2729
void addProperty(Property property);
30+
31+
/**
32+
* Determine if the given attribute belongs to this container.
33+
*/
2834
boolean contains(Property property);
35+
36+
/**
37+
* Get the attribute with the given name belonging to this container.
38+
* @param propertyName the name of an attribute
39+
* @throws MappingException if there is no attribute with the given name
40+
* @since 7.2
41+
*/
42+
Property getProperty(String propertyName) throws MappingException;
43+
44+
/**
45+
* The {@link Table} with the columns mapped by attributes belonging
46+
* to this container.
47+
*/
2948
Table getTable();
3049
}

hibernate-core/src/main/java/org/hibernate/mapping/Component.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
* @author Gavin King
7070
* @author Steve Ebersole
7171
*/
72-
public class Component extends SimpleValue implements MetaAttributable, SortableValue {
72+
public class Component extends SimpleValue implements AttributeContainer, MetaAttributable, SortableValue {
7373

7474
private String componentClassName;
7575
private boolean embedded;
@@ -173,6 +173,7 @@ public void addProperty(Property p, ClassDetails declaringClass) {
173173
}
174174
}
175175

176+
@Override
176177
public void addProperty(Property p) {
177178
addProperty( p, null );
178179
}
@@ -393,6 +394,11 @@ private CompositeUserType<?> createCompositeUserType(Component component) {
393394
: FallbackBeanInstanceProducer.INSTANCE.produceBeanInstance( clazz );
394395
}
395396

397+
@Override
398+
public boolean contains(Property property) {
399+
return properties.contains( property );
400+
}
401+
396402
@Override
397403
public CompositeType getType() throws MappingException {
398404
// Resolve the type of the value once and for all as this operation generates a proxy class
@@ -546,6 +552,7 @@ public Property getProperty(int index) {
546552
return properties.get( index );
547553
}
548554

555+
@Override
549556
public Property getProperty(String propertyName) throws MappingException {
550557
for ( Property prop : properties ) {
551558
if ( prop.getName().equals(propertyName) ) {

hibernate-core/src/main/java/org/hibernate/mapping/Join.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.List;
1010
import java.util.function.Supplier;
1111

12+
import org.hibernate.MappingException;
1213
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
1314
import org.hibernate.jdbc.Expectation;
1415
import org.hibernate.sql.Alias;
@@ -64,6 +65,11 @@ public boolean contains(Property property) {
6465
return properties.contains( property );
6566
}
6667

68+
@Override
69+
public Property getProperty(String propertyName) throws MappingException {
70+
throw new UnsupportedOperationException(); //TODO
71+
}
72+
6773
public void addMappedSuperclassProperty(Property property ) {
6874
properties.add( property );
6975
property.setPersistentClass( persistentClass );
@@ -81,6 +87,7 @@ public boolean containsProperty(Property property) {
8187
return properties.contains( property );
8288
}
8389

90+
@Override
8491
public Table getTable() {
8592
return table;
8693
}

hibernate-core/src/main/java/org/hibernate/mapping/JoinedSubclass.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public JoinedSubclass(PersistentClass superclass, MetadataBuildingContext metada
2525
super( superclass, metadataBuildingContext );
2626
}
2727

28+
@Override
2829
public Table getTable() {
2930
return table;
3031
}

hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,8 @@ else if ( identifierProperty == null && getIdentifierMapper() != null ) {
541541
}
542542
else {
543543
//flat recursive algorithm
544-
property = ( (Component) property.getValue() ).getProperty( element );
544+
final var value = (Component) property.getValue();
545+
property = value.getProperty( element );
545546
}
546547
}
547548
}
@@ -564,6 +565,7 @@ private Property getProperty(String propertyName, List<Property> properties) thr
564565
throw new MappingException( "property [" + propertyName + "] not found on entity [" + getEntityName() + "]" );
565566
}
566567

568+
@Override
567569
public Property getProperty(String propertyName) throws MappingException {
568570
final Property identifierProperty = getIdentifierProperty();
569571
if ( identifierProperty != null

hibernate-core/src/main/java/org/hibernate/mapping/UnionSubclass.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public UnionSubclass(PersistentClass superclass, MetadataBuildingContext buildin
2222
super( superclass, buildingContext );
2323
}
2424

25+
@Override
2526
public Table getTable() {
2627
return table;
2728
}

0 commit comments

Comments
 (0)