Skip to content

Commit 037f081

Browse files
committed
HHH-7702 Add support for collections of (aggregated) composite elements
1 parent 1c992b1 commit 037f081

File tree

6 files changed

+334
-335
lines changed

6 files changed

+334
-335
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public class CompositePluralAttributeElementSourceImpl implements CompositePlura
5252
private List<AttributeSource> attributeSources
5353
= new ArrayList<AttributeSource>();
5454

55+
private String parentReferenceAttributeName;
56+
5557
public CompositePluralAttributeElementSourceImpl(
5658
AssociationAttribute associationAttribute,
5759
RootEntityClass rootEntityClass ) {
@@ -111,8 +113,7 @@ public ValueHolder<Class<?>> getClassReference() {
111113

112114
@Override
113115
public String getParentReferenceAttributeName() {
114-
// TODO: Is this correct?
115-
return associationAttribute.getName();
116+
return parentReferenceAttributeName;
116117
}
117118

118119
@Override
@@ -125,6 +126,9 @@ private void buildAttributeSources() {
125126
EmbeddableClass embeddableClass = rootEntityClass
126127
.getCollectionEmbeddedClasses()
127128
.get( associationAttribute.getName() );
129+
130+
parentReferenceAttributeName = embeddableClass.getParentReferencingAttributeName();
131+
128132
// TODO: Duplicates code in ComponentAttributeSourceImpl.
129133
for ( BasicAttribute attribute : embeddableClass.getSimpleAttributes() ) {
130134
AttributeOverride attributeOverride = null;

hibernate-core/src/test/java/org/hibernate/test/annotations/beanvalidation/HibernateTraversableResolverTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,23 @@
2323
*/
2424
package org.hibernate.test.annotations.beanvalidation;
2525

26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.fail;
28+
2629
import java.math.BigDecimal;
30+
2731
import javax.validation.ConstraintViolation;
2832
import javax.validation.ConstraintViolationException;
2933

30-
import org.junit.Test;
31-
3234
import org.hibernate.Session;
3335
import org.hibernate.Transaction;
3436
import org.hibernate.cfg.Configuration;
35-
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
3637
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
37-
38-
import static org.junit.Assert.assertEquals;
39-
import static org.junit.Assert.fail;
38+
import org.junit.Test;
4039

4140
/**
4241
* @author Emmanuel Bernard
4342
*/
44-
@FailureExpectedWithNewMetamodel
4543
public class HibernateTraversableResolverTest extends BaseCoreFunctionalTestCase {
4644
@Test
4745
public void testNonLazyAssocFieldWithConstraintsFailureExpected() {

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

Lines changed: 105 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.HashSet;
55
import java.util.Map;
66
import java.util.Set;
7+
78
import javax.persistence.AttributeOverride;
89
import javax.persistence.AttributeOverrides;
910
import javax.persistence.CollectionTable;
@@ -34,16 +35,16 @@ public class Boy {
3435
private Integer id;
3536
private String firstName;
3637
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;
38+
private Set<String> nickNames = new HashSet<String>();
39+
private Set<String> hatedNames = new HashSet<String>();
40+
private Set<String> preferredNames = new HashSet<String>();
41+
private Map<String, Integer> scorePerNickName = new HashMap<String, Integer>();
42+
private Map<String, Integer> scorePerPreferredName = new HashMap<String, Integer>();
43+
private int[] favoriteNumbers;
4344
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>();
45+
private Set<Character> characters = new HashSet<Character>();
46+
private Map<String, FavoriteFood> foods = new HashMap<String,FavoriteFood>();
47+
private Set<CountryAttitude> countryAttitudes = new HashSet<CountryAttitude>();
4748

4849
@Id
4950
@GeneratedValue
@@ -71,70 +72,70 @@ public void setLastName(String lastName) {
7172
this.lastName = lastName;
7273
}
7374

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

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

0 commit comments

Comments
 (0)