1616
1717package io .github .kaiso .relmongo .events .callback ;
1818
19+ import com .mongodb .BasicDBList ;
1920import com .mongodb .BasicDBObject ;
2021
2122import io .github .kaiso .relmongo .annotation .FetchType ;
@@ -47,15 +48,15 @@ public void doWith(Field field) throws IllegalAccessException {
4748 FetchType fetchType = null ;
4849 if (field .isAnnotationPresent (OneToMany .class )) {
4950 fetchType = field .getAnnotation (OneToMany .class ).fetch ();
50- loadAssociation (field , fetchType );
51+ loadAssociation (field , fetchType , OneToMany . class );
5152 } else if (field .isAnnotationPresent (OneToOne .class )) {
5253 fetchType = field .getAnnotation (OneToOne .class ).fetch ();
53- loadAssociation (field , fetchType );
54+ loadAssociation (field , fetchType , OneToOne . class );
5455 }
5556
5657 }
5758
58- private void loadAssociation (Field field , FetchType fetchType ) {
59+ private void loadAssociation (Field field , FetchType fetchType , Class <?> annotation ) {
5960 String name = "" ;
6061 try {
6162 name = field .getAnnotation (JoinProperty .class ).name ();
@@ -65,6 +66,12 @@ private void loadAssociation(Field field, FetchType fetchType) {
6566 Object ids = null ;
6667 try {
6768 ids = ((BasicDBObject ) source ).get (name );
69+ if (OneToOne .class .equals (annotation ) && ids instanceof BasicDBList ) {
70+ // mongo database lookups return always an array if unwind is not used event if
71+ // it is a one ot one association
72+ BasicDBList list = (BasicDBList ) ids ;
73+ ids = list .isEmpty () ? list : list .get (0 );
74+ }
6875 } catch (Exception e ) {
6976 throw new IllegalArgumentException ("Property defined in @JoinProperty annotation is not present" , e );
7077 }
0 commit comments