1414* limitations under the License.
1515*/
1616
17- package io .github .kaiso .relmongo ;
17+ package io .github .kaiso .relmongo . events . callback ;
1818
1919import com .mongodb .BasicDBList ;
2020import com .mongodb .BasicDBObject ;
2121import com .mongodb .DBObject ;
2222
2323import io .github .kaiso .relmongo .annotation .JoinProperty ;
2424import io .github .kaiso .relmongo .annotation .OneToMany ;
25+ import io .github .kaiso .relmongo .annotation .OneToOne ;
2526
2627import org .springframework .util .ReflectionUtils ;
2728import org .springframework .util .ReflectionUtils .FieldCallback ;
@@ -40,33 +41,34 @@ public PersistentPropertySavingCallback(Object source) {
4041
4142 public void doWith (Field field ) throws IllegalAccessException {
4243 ReflectionUtils .makeAccessible (field );
44+ if (field .isAnnotationPresent (OneToMany .class ) || field .isAnnotationPresent (OneToOne .class )) {
45+ saveAssociation (field );
46+ }
4347
44- if (field .isAnnotationPresent (OneToMany .class )) {
45- String name = "" ;
46- String referencedPropertyName = "" ;
47- try {
48- name = field .getAnnotation (JoinProperty .class ).name ();
49- referencedPropertyName = field .getAnnotation (JoinProperty .class ).referencedPropertyName ();
50- } catch (Exception e ) {
51- throw new IllegalArgumentException ("Missing or misconfigured @JoinProperty annotation" , e );
52- }
53- if (!"_id" .equals (referencedPropertyName )) {
54- throw new IllegalArgumentException ("in @OneToMany, referencedPropertyName must be allways _id " );
55- }
56- Object ids = null ;
57- try {
58- ids = ((BasicDBObject ) source ).get (name );
59- if (ids instanceof BasicDBList ) {
60- BasicDBList list = new BasicDBList ();
61- list .addAll (((BasicDBList ) ids ).stream ().map (this ::keepOnlyIdentifier ).collect (Collectors .toList ()));
62- ((BasicDBObject ) source ).put (name ,list );
63- }
64- } catch (Exception e ) {
65- throw new IllegalArgumentException ("Property defined in @JoinProperty annotation is not present" , e );
66- }
48+ }
6749
50+ private void saveAssociation (Field field ) {
51+ String name = "" ;
52+ try {
53+ name = field .getAnnotation (JoinProperty .class ).name ();
54+ } catch (Exception e ) {
55+ throw new IllegalArgumentException ("Missing or misconfigured @JoinProperty annotation" , e );
56+ }
57+ Object reference = null ;
58+ try {
59+ reference = ((BasicDBObject ) source ).get (field .getName ());
60+ if (reference instanceof BasicDBList ) {
61+ BasicDBList list = new BasicDBList ();
62+ list .addAll (((BasicDBList ) reference ).stream ().map (this ::keepOnlyIdentifier ).collect (Collectors .toList ()));
63+ ((BasicDBObject ) source ).remove (field .getName ());
64+ ((BasicDBObject ) source ).put (name , list );
65+ } else if (reference instanceof BasicDBObject ) {
66+ ((BasicDBObject ) source ).remove (field .getName ());
67+ ((BasicDBObject ) source ).put (name , ((BasicDBObject ) reference ).get ("_id" ));
68+ }
69+ } catch (Exception e ) {
70+ throw new IllegalArgumentException ("Property defined in @JoinProperty annotation is not present" , e );
6871 }
69-
7072 }
7173
7274 private BasicDBObject keepOnlyIdentifier (Object obj ) {
0 commit comments