Skip to content

Commit 58fd078

Browse files
barreirosebersole
authored andcommitted
HHH-9811 - Enforce uniqueness on EntityPersister#resolveAttributeIndexes
1 parent f795dfc commit 58fd078

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,16 +1869,30 @@ protected String[] getSubclassPropertyNameClosure() {
18691869

18701870
@Override
18711871
public int[] resolveAttributeIndexes(String[] attributeNames) {
1872+
if ( attributeNames == null || attributeNames.length == 0 ) {
1873+
return new int[0];
1874+
}
18721875
int[] fields = new int[attributeNames.length];
18731876
int counter = 0;
18741877

1875-
for ( String attribute : attributeNames ) {
1876-
Integer index = entityMetamodel.getPropertyIndexOrNull( attribute );
1877-
if ( index != null ) {
1878-
fields[counter++] = index;
1878+
// We sort to get rid of duplicates
1879+
Arrays.sort( attributeNames );
1880+
1881+
Integer index0 = entityMetamodel.getPropertyIndexOrNull( attributeNames[0] );
1882+
if ( index0 != null ) {
1883+
fields[counter++] = index0;
1884+
}
1885+
1886+
for ( int i = 0, j = 1; j < attributeNames.length; ++i, ++j ) {
1887+
if ( !attributeNames[i].equals( attributeNames[j] ) ) {
1888+
Integer index = entityMetamodel.getPropertyIndexOrNull( attributeNames[j] );
1889+
if ( index != null ) {
1890+
fields[counter++] = index;
1891+
}
18791892
}
18801893
}
1881-
return fields;
1894+
1895+
return Arrays.copyOf( fields, counter );
18821896
}
18831897

18841898
protected String[] getSubclassPropertySubclassNameClosure() {

hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,13 @@ public Object createProxy(Serializable id, SessionImplementor session)
783783

784784
public FilterAliasGenerator getFilterAliasGenerator(final String rootAlias);
785785

786+
/**
787+
* Converts an array of attribute names to a set of indexes, according to the entity metamodel
788+
*
789+
* @param attributeNames Array of names to be resolved
790+
*
791+
* @return A set of unique indexes of the attribute names found in the metamodel
792+
*/
786793
public int[] resolveAttributeIndexes(String[] attributeNames);
787794

788795
public boolean canUseReferenceCacheEntries();

0 commit comments

Comments
 (0)