31
31
import org .hibernate .engine .spi .CascadeStyle ;
32
32
import org .hibernate .metamodel .internal .source .annotations .attribute .Column ;
33
33
import org .hibernate .metamodel .internal .source .annotations .attribute .PluralAssociationAttribute ;
34
+ import org .hibernate .metamodel .internal .source .annotations .util .EnumConversionHelper ;
35
+ import org .hibernate .metamodel .internal .source .annotations .util .JPADotNames ;
36
+ import org .hibernate .metamodel .internal .source .annotations .util .JandexHelper ;
34
37
import org .hibernate .metamodel .spi .binding .CascadeType ;
38
+ import org .hibernate .metamodel .spi .relational .Value ;
35
39
import org .hibernate .metamodel .spi .source .ForeignKeyContributingSource ;
36
40
import org .hibernate .metamodel .spi .source .ManyToManyPluralAttributeElementSource ;
37
41
import org .hibernate .metamodel .spi .source .RelationalValueSource ;
42
+ import org .jboss .jandex .AnnotationInstance ;
38
43
39
44
/**
40
45
* @author Hardy Ferentschik
46
+ * @author Brett Meyer
41
47
*/
42
48
public class ManyToManyPluralAttributeElementSourceImpl implements ManyToManyPluralAttributeElementSource {
49
+
43
50
private final PluralAssociationAttribute associationAttribute ;
44
-
45
- public ManyToManyPluralAttributeElementSourceImpl (PluralAssociationAttribute associationAttribute ) {
51
+ private final List <RelationalValueSource > relationalValueSources
52
+ = new ArrayList <RelationalValueSource >();
53
+ private final Collection <String > referencedColumnNames
54
+ = new HashSet <String >();
55
+ private final Iterable <CascadeStyle > cascadeStyles ;
56
+
57
+ public ManyToManyPluralAttributeElementSourceImpl (
58
+ PluralAssociationAttribute associationAttribute ) {
46
59
this .associationAttribute = associationAttribute ;
60
+
61
+ for ( Column column : associationAttribute .getJoinColumnValues () ) {
62
+ relationalValueSources .add ( new ColumnSourceImpl (
63
+ associationAttribute , null , column ) );
64
+ }
65
+
66
+ for ( Column column : associationAttribute .getJoinColumnValues () ) {
67
+ if ( column .getReferencedColumnName () != null ) {
68
+ referencedColumnNames .add ( column .getReferencedColumnName () );
69
+ }
70
+ }
71
+
72
+ cascadeStyles = EnumConversionHelper .cascadeTypeToCascadeStyleSet (
73
+ associationAttribute .getCascadeTypes (),
74
+ associationAttribute .getContext () );
47
75
}
48
76
49
77
@ Override
@@ -59,28 +87,16 @@ public String getReferencedEntityAttributeName() {
59
87
60
88
@ Override
61
89
public Collection <String > getReferencedColumnNames () {
62
- HashSet <String > referencedColumnNames = new HashSet <String >();
63
- for ( Column column : associationAttribute .getJoinColumnValues () ) {
64
- if ( column .getReferencedColumnName () != null ) {
65
- referencedColumnNames .add ( column .getReferencedColumnName () );
66
- }
67
- }
68
90
return referencedColumnNames ;
69
91
}
70
92
71
93
@ Override
72
94
public List <RelationalValueSource > relationalValueSources () {
73
- List <RelationalValueSource > valueSources = new ArrayList <RelationalValueSource >();
74
- // todo
75
- return valueSources ;
95
+ return relationalValueSources ;
76
96
}
77
97
78
98
@ Override
79
99
public Iterable <CascadeStyle > getCascadeStyles () {
80
- List <CascadeStyle > cascadeStyles = new ArrayList <CascadeStyle >();
81
- for ( javax .persistence .CascadeType cascadeType : associationAttribute .getCascadeTypes () ) {
82
- cascadeStyles .add ( CascadeType .getCascadeType ( cascadeType ).toCascadeStyle () );
83
- }
84
100
return cascadeStyles ;
85
101
}
86
102
@@ -96,7 +112,8 @@ public String getExplicitForeignKeyName() {
96
112
97
113
@ Override
98
114
public JoinColumnResolutionDelegate getForeignKeyTargetColumnResolutionDelegate () {
99
- return null ; //To change body of implemented methods use File | Settings | File Templates.
115
+ return associationAttribute .getJoinColumnValues ()
116
+ .isEmpty () ? null : new AnnotationJoinColumnResolutionDelegate ();
100
117
}
101
118
102
119
@ Override
@@ -138,6 +155,55 @@ public boolean areValuesIncludedInUpdateByDefault() {
138
155
public boolean areValuesNullableByDefault () {
139
156
return false ;
140
157
}
158
+
159
+ // TODO: Taken from and in duplicate of ToOneAttributeSourceImpl. Look into
160
+ // abstracting some of this out.
161
+ public class AnnotationJoinColumnResolutionDelegate
162
+ implements ForeignKeyContributingSource .JoinColumnResolutionDelegate {
163
+ private final String logicalJoinTableName ;
164
+
165
+ public AnnotationJoinColumnResolutionDelegate () {
166
+ logicalJoinTableName = resolveLogicalJoinTableName ();
167
+ }
168
+
169
+ @ Override
170
+ public List <Value > getJoinColumns (JoinColumnResolutionContext context ) {
171
+ final List <Value > values = new ArrayList <Value >();
172
+ for ( Column column : associationAttribute .getJoinColumnValues () ) {
173
+ if ( column .getReferencedColumnName () == null ) {
174
+ return context .resolveRelationalValuesForAttribute ( null );
175
+ }
176
+ org .hibernate .metamodel .spi .relational .Column resolvedColumn = context .resolveColumn (
177
+ column .getReferencedColumnName (),
178
+ logicalJoinTableName ,
179
+ null ,
180
+ null
181
+ );
182
+ values .add ( resolvedColumn );
183
+ }
184
+ return values ;
185
+ }
186
+
187
+ @ Override
188
+ public String getReferencedAttributeName () {
189
+ // in annotations we are not referencing attribute but column names via @JoinColumn(s)
190
+ return null ;
191
+ }
192
+
193
+ private String resolveLogicalJoinTableName () {
194
+ final AnnotationInstance joinTableAnnotation = JandexHelper .getSingleAnnotation (
195
+ associationAttribute .annotations (),
196
+ JPADotNames .JOIN_TABLE
197
+ );
198
+
199
+ if ( joinTableAnnotation != null ) {
200
+ return JandexHelper .getValue ( joinTableAnnotation , "name" , String .class );
201
+ }
202
+
203
+ // todo : this ties into the discussion about naming strategies. This would be part of a logical naming strategy...
204
+ return null ;
205
+ }
206
+ }
141
207
}
142
208
143
209
0 commit comments