25
25
26
26
import net .tsz .afinal .db .sqlite .CursorUtils ;
27
27
import net .tsz .afinal .db .sqlite .DbModel ;
28
+ import net .tsz .afinal .db .sqlite .ManyToOneLazyLoader ;
28
29
import net .tsz .afinal .db .sqlite .OneToManyLazyLoader ;
29
30
import net .tsz .afinal .db .sqlite .SqlBuilder ;
30
31
import net .tsz .afinal .db .sqlite .SqlInfo ;
@@ -373,7 +374,7 @@ public <T> T findWithManyToOneById(Object id ,Class<T> clazz){
373
374
DbModel dbModel = findDbModelBySQL (sql );
374
375
if (dbModel !=null ){
375
376
T entity = CursorUtils .dbModel2Entity (dbModel , clazz );
376
- return loadManyToOne (entity ,clazz );
377
+ return loadManyToOne (dbModel , entity ,clazz );
377
378
}
378
379
379
380
return null ;
@@ -393,24 +394,33 @@ public <T> T findWithManyToOneById(Object id ,Class<T> clazz,Class<?> ... findCl
393
394
DbModel dbModel = findDbModelBySQL (sql );
394
395
if (dbModel !=null ){
395
396
T entity = CursorUtils .dbModel2Entity (dbModel , clazz );
396
- return loadManyToOne (entity ,clazz ,findClass );
397
+ return loadManyToOne (dbModel , entity ,clazz ,findClass );
397
398
}
398
399
return null ;
399
400
}
400
401
401
402
/**
402
403
* 将entity中的“多对一”的数据填充满
404
+ * 如果是懒加载填充,则dbModel参数可为null
403
405
* @param clazz
404
406
* @param entity
405
407
* @param <T>
406
408
* @return
407
409
*/
408
- public <T > T loadManyToOne (T entity ,Class <T > clazz ,Class <?> ... findClass ) {
410
+ public <T > T loadManyToOne (DbModel dbModel , T entity ,Class <T > clazz ,Class <?> ... findClass ) {
409
411
if (entity !=null ){
410
412
try {
411
413
Collection <ManyToOne > manys = TableInfo .get (clazz ).manyToOneMap .values ();
412
414
for (ManyToOne many : manys ){
413
- Object id = many .getValue (entity );
415
+
416
+ Object id = null ;
417
+ if (dbModel !=null ){
418
+ dbModel .get (many .getColumn ());
419
+ }else if (many .getValue (entity ).getClass ()== ManyToOneLazyLoader .class
420
+ &&many .getValue (entity )!=null ){
421
+ id = ((ManyToOneLazyLoader )many .getValue (entity )).getFieldValue ();
422
+ }
423
+
414
424
if (id !=null ){
415
425
boolean isFind = false ;
416
426
if (findClass == null || findClass .length ==0 ){
@@ -423,10 +433,19 @@ public <T> T loadManyToOne(T entity,Class<T> clazz,Class<?> ... findClass) {
423
433
}
424
434
}
425
435
if (isFind ){
436
+
426
437
@ SuppressWarnings ("unchecked" )
427
- T manyEntity = (T ) findById (Integer .valueOf (id .toString ()), many .getDataType ());
438
+ T manyEntity = (T ) findById (Integer .valueOf (id .toString ()), many .getManyClass ());
428
439
if (manyEntity !=null ){
429
- many .setValue (entity , manyEntity );
440
+ if (many .getValue (entity ).getClass ()== ManyToOneLazyLoader .class ){
441
+ if (many .getValue (entity )==null ){
442
+ many .setValue (entity ,new ManyToOneLazyLoader (entity ,clazz ,many .getManyClass (),this ));
443
+ }
444
+ ((ManyToOneLazyLoader )many .getValue (entity )).set (manyEntity );
445
+ }else {
446
+ many .setValue (entity , manyEntity );
447
+ }
448
+
430
449
}
431
450
}
432
451
}
0 commit comments