6
6
*/
7
7
package org .hibernate .boot .model .source .internal .hbm ;
8
8
9
+ import java .io .Serializable ;
10
+ import java .util .ArrayList ;
11
+ import java .util .List ;
9
12
import java .util .Optional ;
10
13
11
14
import org .hibernate .AssertionFailure ;
12
15
import org .hibernate .boot .MappingException ;
16
+ import org .hibernate .boot .jaxb .hbm .spi .JaxbHbmColumnType ;
13
17
import org .hibernate .boot .jaxb .hbm .spi .JaxbHbmFilterType ;
14
18
import org .hibernate .boot .jaxb .hbm .spi .JaxbHbmManyToOneType ;
15
19
import org .hibernate .boot .jaxb .hbm .spi .JaxbHbmRootEntityType ;
@@ -72,17 +76,58 @@ protected AbstractPluralAttributeSourceImpl(
72
76
73
77
Optional <JaxbHbmManyToOneType > jaxbHbmManyToOneTypeOptional = Optional .empty ();
74
78
75
- if ( pluralAttributeJaxbMapping .isInverse () && pluralAttributeJaxbMapping .getOneToMany () != null ) {
79
+ // Our goal here is to find the inverse side of a one to many to figure out against what to join
80
+ if ( pluralAttributeJaxbMapping .isInverse () && pluralAttributeJaxbMapping .getOneToMany () != null && pluralAttributeJaxbMapping .getKey ().getPropertyRef () == null ) {
76
81
String childClass = pluralAttributeJaxbMapping .getOneToMany ().getClazz ();
77
82
78
83
if ( childClass != null ) {
84
+ // We match by columns as defined in the key
85
+ final List <String > keyColumnNames ;
86
+ if ( pluralAttributeJaxbMapping .getKey ().getColumnAttribute () == null ) {
87
+ keyColumnNames = new ArrayList <>( pluralAttributeJaxbMapping .getKey ().getColumn ().size () );
88
+ for ( JaxbHbmColumnType jaxbHbmColumnType : pluralAttributeJaxbMapping .getKey ().getColumn () ) {
89
+ keyColumnNames .add ( jaxbHbmColumnType .getName () );
90
+ }
91
+ }
92
+ else {
93
+ keyColumnNames = new ArrayList <>( 1 );
94
+ keyColumnNames .add ( pluralAttributeJaxbMapping .getKey ().getColumnAttribute () );
95
+ }
79
96
jaxbHbmManyToOneTypeOptional = mappingDocument .getDocumentRoot ().getClazz ()
80
97
.stream ()
81
98
.filter ( (JaxbHbmRootEntityType entityType ) -> childClass .equals ( entityType .getName () ) )
82
99
.flatMap ( jaxbHbmRootEntityType -> jaxbHbmRootEntityType .getAttributes ().stream () )
83
- .filter (
84
- attribute -> attribute instanceof JaxbHbmManyToOneType &&
85
- ( (JaxbHbmManyToOneType ) attribute ).getPropertyRef () != null )
100
+ .filter ( attribute -> {
101
+ if ( attribute instanceof JaxbHbmManyToOneType ) {
102
+ JaxbHbmManyToOneType manyToOneType = (JaxbHbmManyToOneType ) attribute ;
103
+ String manyToOneTypeClass = manyToOneType .getClazz ();
104
+ String containerClass = container .getAttributeRoleBase ().getFullPath ();
105
+ // Consider many to ones that have no class defined or equal the owner class of the one to many
106
+ if ( manyToOneTypeClass == null || manyToOneTypeClass .equals ( containerClass ) ) {
107
+ if ( manyToOneType .getColumnAttribute () == null ) {
108
+ List <Serializable > columns = manyToOneType .getColumnOrFormula ();
109
+ if ( columns .size () != keyColumnNames .size () ) {
110
+ return false ;
111
+ }
112
+ for ( int i = 0 ; i < columns .size (); i ++ ) {
113
+ Serializable column = columns .get ( i );
114
+ String keyColumn = keyColumnNames .get ( i );
115
+ if ( !( column instanceof JaxbHbmColumnType ) || !( (JaxbHbmColumnType ) column )
116
+ .getName ()
117
+ .equals ( keyColumn ) ) {
118
+ return false ;
119
+ }
120
+ }
121
+ }
122
+ else {
123
+ return keyColumnNames .size () == 1 && keyColumnNames .get ( 0 )
124
+ .equals ( manyToOneType .getColumnAttribute () );
125
+ }
126
+ return true ;
127
+ }
128
+ }
129
+ return false ;
130
+ })
86
131
.map ( JaxbHbmManyToOneType .class ::cast )
87
132
.findFirst ();
88
133
}
@@ -91,13 +136,12 @@ protected AbstractPluralAttributeSourceImpl(
91
136
this .keySource = jaxbHbmManyToOneTypeOptional
92
137
.map ( jaxbHbmManyToOneType -> new PluralAttributeKeySourceImpl (
93
138
sourceMappingDocument (),
139
+ pluralAttributeJaxbMapping .getKey (),
94
140
jaxbHbmManyToOneType ,
95
141
container
96
142
) ).orElseGet ( () -> new PluralAttributeKeySourceImpl (
97
143
sourceMappingDocument (),
98
- pluralAttributeJaxbMapping .isInverse () ?
99
- pluralAttributeJaxbMapping .getKey () :
100
- pluralAttributeJaxbMapping .getKey (),
144
+ pluralAttributeJaxbMapping .getKey (),
101
145
container
102
146
) );
103
147
0 commit comments