Skip to content

Commit d1ba070

Browse files
committed
HHH-7851 - basic collection is not indexable yet (annotaiton side)
1 parent 1b24693 commit d1ba070

File tree

4 files changed

+62
-48
lines changed

4 files changed

+62
-48
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/EntitySourceImpl.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.hibernate.metamodel.spi.source.JpaCallbackSource;
5454
import org.hibernate.metamodel.spi.source.LocalBindingContext;
5555
import org.hibernate.metamodel.spi.source.MetaAttributeSource;
56+
import org.hibernate.metamodel.spi.source.PluralAttributeSource;
5657
import org.hibernate.metamodel.spi.source.PrimaryKeyJoinColumnSource;
5758
import org.hibernate.metamodel.spi.source.SecondaryTableSource;
5859
import org.hibernate.metamodel.spi.source.SubclassEntitySource;
@@ -238,14 +239,14 @@ public List<AttributeSource> attributeSources() {
238239
}
239240
case MANY_TO_MANY:
240241
case ONE_TO_MANY:
241-
AttributeSource source = ((PluralAssociationAttribute)associationAttribute).isIndexed() ?
242-
new IndexedPluralAttributeSourceImpl((PluralAssociationAttribute) associationAttribute, entityClass )
243-
:new PluralAttributeSourceImpl( ( PluralAssociationAttribute ) associationAttribute, entityClass );
244-
attributeList.add( source );
245-
break;
246242
case ELEMENT_COLLECTION_BASIC:
247243
case ELEMENT_COLLECTION_EMBEDDABLE: {
248-
source = new PluralAttributeSourceImpl( ( PluralAssociationAttribute ) associationAttribute, entityClass );
244+
PluralAssociationAttribute pluralAssociationAttribute = (PluralAssociationAttribute) associationAttribute;
245+
PluralAttributeSource.Nature pluralAttributeNature = pluralAssociationAttribute.getPluralAttributeNature();
246+
247+
AttributeSource source = pluralAssociationAttribute.isIndexed() ?
248+
new IndexedPluralAttributeSourceImpl( pluralAssociationAttribute, entityClass )
249+
: new PluralAttributeSourceImpl( pluralAssociationAttribute, entityClass );
249250
attributeList.add( source );
250251
break;
251252
}

hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/IndexedPluralAttributeSourceImpl.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.hibernate.metamodel.internal.source.annotations;
22

3+
import java.util.EnumSet;
4+
35
import org.hibernate.metamodel.internal.source.annotations.attribute.MappedAttribute;
46
import org.hibernate.metamodel.internal.source.annotations.attribute.PluralAssociationAttribute;
57
import org.hibernate.metamodel.internal.source.annotations.entity.EntityClass;
@@ -13,17 +15,16 @@
1315
public class IndexedPluralAttributeSourceImpl extends PluralAttributeSourceImpl
1416
implements IndexedPluralAttributeSource {
1517
private final PluralAttributeIndexSource indexSource;
18+
private final static EnumSet<MappedAttribute.Nature> validNatures = EnumSet.of(
19+
MappedAttribute.Nature.MANY_TO_MANY,
20+
MappedAttribute.Nature.ONE_TO_MANY,
21+
MappedAttribute.Nature.ELEMENT_COLLECTION_BASIC,
22+
MappedAttribute.Nature.ELEMENT_COLLECTION_EMBEDDABLE);
1623

1724
public IndexedPluralAttributeSourceImpl(PluralAssociationAttribute attribute,
1825
EntityClass entityClass ) {
1926
super( attribute, entityClass );
20-
if ( getNature() == org.hibernate.metamodel.spi.source.PluralAttributeSource.Nature.SET || getNature() == org.hibernate.metamodel.spi.source.PluralAttributeSource.Nature.MAP ) {
21-
throw new MappingException(
22-
"Set / Map could not be an indexed column",
23-
attribute.getContext().getOrigin()
24-
);
25-
}
26-
if ( attribute.getNature() != MappedAttribute.Nature.MANY_TO_MANY && attribute.getNature() != MappedAttribute.Nature.ONE_TO_MANY ) {
27+
if ( !validNatures.contains( attribute.getNature() ) ) {
2728
throw new MappingException(
2829
"Indexed column could be only mapped on the MANY side",
2930
attribute.getContext().getOrigin()

hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/PluralAttributeSourceImpl.java

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.Map;
2929
import java.util.Set;
3030

31+
import org.jboss.jandex.AnnotationInstance;
32+
3133
import org.hibernate.engine.FetchStyle;
3234
import org.hibernate.engine.FetchTiming;
3335
import org.hibernate.internal.util.StringHelper;
@@ -62,11 +64,11 @@ public PluralAttributeSourceImpl(
6264
final PluralAssociationAttribute associationAttribute,
6365
final EntityClass entityClass ) {
6466
this.associationAttribute = associationAttribute;
65-
this.nature = resolveAttributeNature();
6667
this.entityClass = entityClass;
6768
this.keySource = new PluralAttributeKeySourceImpl( associationAttribute );
68-
this.elementSource = determineElementSource();
6969
this.typeSource = new ExplicitHibernateTypeSourceImpl( associationAttribute );
70+
this.nature = associationAttribute.getPluralAttributeNature();
71+
this.elementSource = determineElementSource( associationAttribute, entityClass );
7072
}
7173

7274
@Override
@@ -91,12 +93,7 @@ public ValueHolder<Class<?>> getElementClassReference() {
9193
}
9294
}
9395

94-
@Override
95-
public String getMappedBy() {
96-
return associationAttribute.getMappedBy();
97-
}
98-
99-
private PluralAttributeElementSource determineElementSource() {
96+
private static PluralAttributeElementSource determineElementSource(PluralAssociationAttribute associationAttribute, EntityClass entityClass) {
10097
switch ( associationAttribute.getNature() ) {
10198
case MANY_TO_MANY:
10299
return new ManyToManyPluralAttributeElementSourceImpl( associationAttribute );
@@ -109,7 +106,8 @@ private PluralAttributeElementSource determineElementSource() {
109106
case ELEMENT_COLLECTION_EMBEDDABLE: {
110107
// TODO: cascadeStyles?
111108
return new CompositePluralAttributeElementSourceImpl(
112-
associationAttribute, entityClass );
109+
associationAttribute, entityClass
110+
);
113111
}
114112
}
115113
throw new AssertionError( "Unexpected attribute nature for a association:" + associationAttribute.getNature() );
@@ -124,13 +122,8 @@ public PluralAttributeKeySource getKeySource() {
124122
public TableSpecificationSource getCollectionTableSpecificationSource() {
125123
// todo - see org.hibernate.metamodel.internal.Binder#bindOneToManyCollectionKey
126124
// todo - needs to cater for @CollectionTable and @JoinTable
127-
128-
if ( associationAttribute.getJoinTableAnnotation() == null ) {
129-
return null;
130-
}
131-
else {
132-
return new TableSourceImpl( associationAttribute.getJoinTableAnnotation() );
133-
}
125+
final AnnotationInstance joinTableAnnotation = associationAttribute.getJoinTableAnnotation();
126+
return joinTableAnnotation == null ? null : new TableSourceImpl( joinTableAnnotation );
134127
}
135128

136129
@Override
@@ -158,9 +151,14 @@ public String getWhere() {
158151
return associationAttribute.getWhereClause();
159152
}
160153

154+
@Override
155+
public String getMappedBy() {
156+
return associationAttribute.getMappedBy();
157+
}
158+
161159
@Override
162160
public boolean isInverse() {
163-
return associationAttribute.getMappedBy() != null;
161+
return getMappedBy() != null;
164162
}
165163

166164
@Override
@@ -244,33 +242,20 @@ public FetchTiming getFetchTiming() {
244242
if ( associationAttribute.isExtraLazy() ) {
245243
return FetchTiming.EXTRA_DELAYED;
246244
}
247-
if ( associationAttribute.isLazy() ) {
245+
else if ( associationAttribute.isLazy() ) {
248246
return FetchTiming.DELAYED;
249247
}
250-
return FetchTiming.IMMEDIATE;
248+
else {
249+
return FetchTiming.IMMEDIATE;
250+
}
251251
}
252252

253253
@Override
254254
public FetchStyle getFetchStyle() {
255255
return associationAttribute.getFetchStyle();
256256
}
257257

258-
private Nature resolveAttributeNature() {
259-
if ( Map.class.isAssignableFrom( associationAttribute.getAttributeType() ) ) {
260-
return PluralAttributeSource.Nature.MAP;
261-
}
262-
else if ( List.class.isAssignableFrom( associationAttribute.getAttributeType() ) ) {
263-
return associationAttribute.isIndexed()? Nature.LIST : Nature.BAG;
264-
}
265-
else if ( Set.class.isAssignableFrom( associationAttribute.getAttributeType() ) ) {
266-
return PluralAttributeSource.Nature.SET;
267-
} else if ( associationAttribute.getAttributeType().isArray() ) {
268-
return PluralAttributeSource.Nature.ARRAY;
269-
}
270-
else {
271-
return PluralAttributeSource.Nature.BAG;
272-
}
273-
}
258+
274259
}
275260

276261

hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/annotations/attribute/PluralAssociationAttribute.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.util.List;
2727
import java.util.Map;
28+
import java.util.Set;
2829

2930
import javax.persistence.FetchType;
3031

@@ -47,6 +48,7 @@
4748
import org.hibernate.metamodel.spi.binding.Caching;
4849
import org.hibernate.metamodel.spi.binding.CustomSQL;
4950
import org.hibernate.metamodel.spi.source.MappingException;
51+
import org.hibernate.metamodel.spi.source.PluralAttributeSource;
5052

5153
/**
5254
* Represents an collection (collection, list, set, map) association attribute.
@@ -74,6 +76,8 @@ public class PluralAssociationAttribute extends AssociationAttribute {
7476
private final String inverseForeignKeyName;
7577
private final String explicitForeignKeyName;
7678

79+
private final PluralAttributeSource.Nature pluralAttributeNature;
80+
7781
private LazyCollectionOption lazyOption;
7882

7983
public static PluralAssociationAttribute createPluralAssociationAttribute(ClassInfo entityClassInfo,
@@ -96,6 +100,10 @@ public static PluralAssociationAttribute createPluralAssociationAttribute(ClassI
96100
);
97101
}
98102

103+
public PluralAttributeSource.Nature getPluralAttributeNature() {
104+
return pluralAttributeNature;
105+
}
106+
99107
public String getWhereClause() {
100108
return whereClause;
101109
}
@@ -240,7 +248,26 @@ HibernateDotNames.SQL_DELETE_ALL, annotations()
240248
);
241249
}
242250
this.isIndexed = orderColumnAnnotation != null || indexColumnAnnotation != null;
251+
this.pluralAttributeNature = resolvePluralAttributeNature();
252+
}
253+
254+
private PluralAttributeSource.Nature resolvePluralAttributeNature() {
243255

256+
if ( Map.class.isAssignableFrom( getAttributeType() ) ) {
257+
return PluralAttributeSource.Nature.MAP;
258+
}
259+
else if ( List.class.isAssignableFrom( getAttributeType() ) ) {
260+
return isIndexed() ? PluralAttributeSource.Nature.LIST : PluralAttributeSource.Nature.BAG;
261+
}
262+
else if ( Set.class.isAssignableFrom( getAttributeType() ) ) {
263+
return PluralAttributeSource.Nature.SET;
264+
}
265+
else if ( getAttributeType().isArray() ) {
266+
return PluralAttributeSource.Nature.ARRAY;
267+
}
268+
else {
269+
return PluralAttributeSource.Nature.BAG;
270+
}
244271
}
245272

246273
private OnDeleteAction determineOnDeleteAction() {

0 commit comments

Comments
 (0)