Skip to content

Commit 36120a0

Browse files
committed
HHH-7702 Add support for collections of (aggregated) composite elements
1 parent e19513c commit 36120a0

File tree

5 files changed

+320
-310
lines changed

5 files changed

+320
-310
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/MetadataSources.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import java.io.IOException;
3030
import java.io.InputStream;
3131
import java.lang.reflect.Field;
32+
import java.lang.reflect.ParameterizedType;
3233
import java.net.URL;
3334
import java.util.ArrayList;
35+
import java.util.Collection;
3436
import java.util.Enumeration;
3537
import java.util.HashSet;
3638
import java.util.LinkedHashSet;
@@ -520,17 +522,26 @@ private void indexClass(Class clazz, Indexer indexer, Set<Class<?>> processedCla
520522
// For backward compatibility, don't require @Embeddable
521523
// classes to be explicitly identified.
522524
// Automatically find them by checking the fields' types.
523-
// TODO: There's got to be a better way to do this. Should it be moved?
525+
// TODO: There has to be a better way to do this.
524526
for ( Field declaredField : clazz.getDeclaredFields() ) {
525527
Class<?> fieldClass = declaredField.getType();
528+
if ( fieldClass.isArray() ) {
529+
fieldClass = fieldClass.getComponentType();
530+
}
531+
else if ( Collection.class.isAssignableFrom( fieldClass ) ) {
532+
ParameterizedType listType = (ParameterizedType) declaredField.getGenericType();
533+
fieldClass = (Class<?>) listType.getActualTypeArguments()[0];
534+
}
535+
// TODO: Map
536+
526537
if ( !fieldClass.isPrimitive() && fieldClass != Object.class ) {
527538
try {
528539
Index fieldIndex = JandexHelper.indexForClass(
529540
serviceRegistry.getService( ClassLoaderService.class ),
530541
fieldClass );
531542
if ( !fieldIndex.getAnnotations(
532543
JPADotNames.EMBEDDABLE ).isEmpty() ) {
533-
indexClass( declaredField.getType(), indexer, processedClasses );
544+
indexClass( fieldClass, indexer, processedClasses );
534545
}
535546
} catch ( Exception e ) {
536547
// do nothing

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public class CompositePluralAttributeElementSourceImpl implements CompositePlura
5353

5454
public CompositePluralAttributeElementSourceImpl(
5555
AssociationAttribute associationAttribute,
56-
RootEntityClass rootEntityClass,
57-
Iterable<CascadeStyle> cascadeStyles ) {
56+
RootEntityClass rootEntityClass ) {
5857
this.associationAttribute = associationAttribute;
5958
this.rootEntityClass = rootEntityClass;
6059

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private PluralAttributeElementSource determineElementSource() {
103103
case ELEMENT_COLLECTION_EMBEDDABLE: {
104104
// TODO: cascadeStyles?
105105
return new CompositePluralAttributeElementSourceImpl(
106-
associationAttribute, (RootEntityClass) entityClass, null );
106+
associationAttribute, (RootEntityClass) entityClass );
107107
}
108108
}
109109
throw new AssertionError( "Unexpected attribute nature for a association:" + associationAttribute.getNature() );

hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/Boy.java

Lines changed: 104 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ public class Boy {
3434
private Integer id;
3535
private String firstName;
3636
private String lastName;
37-
private Set<String> nickNames = new HashSet<String>();
38-
private Set<String> hatedNames = new HashSet<String>();
39-
private Set<String> preferredNames = new HashSet<String>();
40-
private Map<String, Integer> scorePerNickName = new HashMap<String, Integer>();
41-
private Map<String, Integer> scorePerPreferredName = new HashMap<String, Integer>();
42-
private int[] favoriteNumbers;
37+
// private Set<String> nickNames = new HashSet<String>();
38+
// private Set<String> hatedNames = new HashSet<String>();
39+
// private Set<String> preferredNames = new HashSet<String>();
40+
// private Map<String, Integer> scorePerNickName = new HashMap<String, Integer>();
41+
// private Map<String, Integer> scorePerPreferredName = new HashMap<String, Integer>();
42+
// private int[] favoriteNumbers;
4343
private Set<Toy> favoriteToys = new HashSet<Toy>();
44-
private Set<Character> characters = new HashSet<Character>();
45-
private Map<String, FavoriteFood> foods = new HashMap<String,FavoriteFood>();
46-
private Set<CountryAttitude> countryAttitudes = new HashSet<CountryAttitude>();
44+
// private Set<Character> characters = new HashSet<Character>();
45+
// private Map<String, FavoriteFood> foods = new HashMap<String,FavoriteFood>();
46+
// private Set<CountryAttitude> countryAttitudes = new HashSet<CountryAttitude>();
4747

4848
@Id
4949
@GeneratedValue
@@ -71,70 +71,70 @@ public void setLastName(String lastName) {
7171
this.lastName = lastName;
7272
}
7373

74-
@ElementCollection
75-
public Set<String> getNickNames() {
76-
return nickNames;
77-
}
78-
79-
public void setNickNames(Set<String> nickName) {
80-
this.nickNames = nickName;
81-
}
82-
83-
@ElementCollection //default column names
84-
public Set<String> getHatedNames() {
85-
return hatedNames;
86-
}
87-
88-
public void setHatedNames(Set<String> hatedNames) {
89-
this.hatedNames = hatedNames;
90-
}
91-
92-
@ElementCollection //default column names
93-
@Column
94-
public Set<String> getPreferredNames() {
95-
return preferredNames;
96-
}
97-
98-
public void setPreferredNames(Set<String> preferredNames) {
99-
this.preferredNames = preferredNames;
100-
}
101-
102-
@ElementCollection
103-
@MapKeyColumn(nullable=false)
104-
public Map<String, Integer> getScorePerPreferredName() {
105-
return scorePerPreferredName;
106-
}
107-
108-
public void setScorePerPreferredName(Map<String, Integer> scorePerPreferredName) {
109-
this.scorePerPreferredName = scorePerPreferredName;
110-
}
111-
112-
@ElementCollection
113-
@CollectionTable(name = "ScorePerNickName", joinColumns = @JoinColumn(name = "BoyId"))
114-
@Column(name = "score", nullable = false)
115-
@MapKeyColumn(nullable=false)
116-
public Map<String, Integer> getScorePerNickName() {
117-
return scorePerNickName;
118-
}
119-
120-
public void setScorePerNickName(Map<String, Integer> scorePerNickName) {
121-
this.scorePerNickName = scorePerNickName;
122-
}
123-
124-
@ElementCollection
125-
@CollectionTable(
126-
name = "BoyFavoriteNumbers",
127-
joinColumns = @JoinColumn(name = "BoyId")
128-
)
129-
@Column(name = "favoriteNumber", nullable = false)
130-
@OrderColumn(name = "nbr_index")
131-
public int[] getFavoriteNumbers() {
132-
return favoriteNumbers;
133-
}
134-
135-
public void setFavoriteNumbers(int[] favoriteNumbers) {
136-
this.favoriteNumbers = favoriteNumbers;
137-
}
74+
// @ElementCollection
75+
// public Set<String> getNickNames() {
76+
// return nickNames;
77+
// }
78+
//
79+
// public void setNickNames(Set<String> nickName) {
80+
// this.nickNames = nickName;
81+
// }
82+
//
83+
// @ElementCollection //default column names
84+
// public Set<String> getHatedNames() {
85+
// return hatedNames;
86+
// }
87+
//
88+
// public void setHatedNames(Set<String> hatedNames) {
89+
// this.hatedNames = hatedNames;
90+
// }
91+
//
92+
// @ElementCollection //default column names
93+
// @Column
94+
// public Set<String> getPreferredNames() {
95+
// return preferredNames;
96+
// }
97+
//
98+
// public void setPreferredNames(Set<String> preferredNames) {
99+
// this.preferredNames = preferredNames;
100+
// }
101+
//
102+
// @ElementCollection
103+
// @MapKeyColumn(nullable=false)
104+
// public Map<String, Integer> getScorePerPreferredName() {
105+
// return scorePerPreferredName;
106+
// }
107+
//
108+
// public void setScorePerPreferredName(Map<String, Integer> scorePerPreferredName) {
109+
// this.scorePerPreferredName = scorePerPreferredName;
110+
// }
111+
//
112+
// @ElementCollection
113+
// @CollectionTable(name = "ScorePerNickName", joinColumns = @JoinColumn(name = "BoyId"))
114+
// @Column(name = "score", nullable = false)
115+
// @MapKeyColumn(nullable=false)
116+
// public Map<String, Integer> getScorePerNickName() {
117+
// return scorePerNickName;
118+
// }
119+
//
120+
// public void setScorePerNickName(Map<String, Integer> scorePerNickName) {
121+
// this.scorePerNickName = scorePerNickName;
122+
// }
123+
//
124+
// @ElementCollection
125+
// @CollectionTable(
126+
// name = "BoyFavoriteNumbers",
127+
// joinColumns = @JoinColumn(name = "BoyId")
128+
// )
129+
// @Column(name = "favoriteNumber", nullable = false)
130+
// @OrderColumn(name = "nbr_index")
131+
// public int[] getFavoriteNumbers() {
132+
// return favoriteNumbers;
133+
// }
134+
//
135+
// public void setFavoriteNumbers(int[] favoriteNumbers) {
136+
// this.favoriteNumbers = favoriteNumbers;
137+
// }
138138
@ElementCollection
139139
@AttributeOverride(name = "element.serial", column = @Column(name = "serial_nbr"))
140140
public Set<Toy> getFavoriteToys() {
@@ -145,36 +145,36 @@ public void setFavoriteToys(Set<Toy> favoriteToys) {
145145
this.favoriteToys = favoriteToys;
146146
}
147147

148-
@ElementCollection
149-
@Enumerated(EnumType.STRING)
150-
@Column(name = "`characters`")
151-
public Set<Character> getCharacters() {
152-
return characters;
153-
}
154-
155-
public void setCharacters(Set<Character> characters) {
156-
this.characters = characters;
157-
}
158-
159-
@ElementCollection
160-
@Enumerated(EnumType.STRING)
161-
@MapKeyColumn(nullable=false)
162-
public Map<String, FavoriteFood> getFavoriteFood() {
163-
return foods;
164-
}
165-
166-
public void setFavoriteFood(Map<String, FavoriteFood>foods) {
167-
this.foods = foods;
168-
}
169-
170-
@ElementCollection(fetch = FetchType.EAGER)
171-
//@Where(clause = "b_likes=false")
172-
public Set<CountryAttitude> getCountryAttitudes() {
173-
return countryAttitudes;
174-
}
175-
176-
public void setCountryAttitudes(Set<CountryAttitude> countryAttitudes) {
177-
this.countryAttitudes = countryAttitudes;
178-
}
148+
// @ElementCollection
149+
// @Enumerated(EnumType.STRING)
150+
// @Column(name = "`characters`")
151+
// public Set<Character> getCharacters() {
152+
// return characters;
153+
// }
154+
//
155+
// public void setCharacters(Set<Character> characters) {
156+
// this.characters = characters;
157+
// }
158+
//
159+
// @ElementCollection
160+
// @Enumerated(EnumType.STRING)
161+
// @MapKeyColumn(nullable=false)
162+
// public Map<String, FavoriteFood> getFavoriteFood() {
163+
// return foods;
164+
// }
165+
//
166+
// public void setFavoriteFood(Map<String, FavoriteFood>foods) {
167+
// this.foods = foods;
168+
// }
169+
//
170+
// @ElementCollection(fetch = FetchType.EAGER)
171+
// //@Where(clause = "b_likes=false")
172+
// public Set<CountryAttitude> getCountryAttitudes() {
173+
// return countryAttitudes;
174+
// }
175+
//
176+
// public void setCountryAttitudes(Set<CountryAttitude> countryAttitudes) {
177+
// this.countryAttitudes = countryAttitudes;
178+
// }
179179
}
180180

0 commit comments

Comments
 (0)