Skip to content

Commit 7fd62e4

Browse files
authored
Merge pull request #20 from kaiso/support/feature/#14
[fix] do not override attribute when can not fetch child collection
2 parents 18fc11b + 0373653 commit 7fd62e4

4 files changed

Lines changed: 33 additions & 21 deletions

File tree

src/main/java/io/github/kaiso/relmongo/events/callback/PersistentPropertyLazyLoadingCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void doWith(Field field) throws IllegalAccessException {
7171

7272
Object relations = document.get(joinPropertyName);
7373
List<Object> identifierList = new ArrayList<>();
74-
if (relations == null) {
74+
if (relations == null || ((DBObject)relations).keySet().isEmpty()) {
7575
return;
7676
}
7777
if (relations instanceof Collection) {

src/main/java/io/github/kaiso/relmongo/events/callback/PersistentPropertyLoadingCallback.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.mongodb.BasicDBList;
2020
import com.mongodb.BasicDBObject;
21+
import com.mongodb.DBObject;
2122

2223
import io.github.kaiso.relmongo.annotation.FetchType;
2324
import io.github.kaiso.relmongo.annotation.JoinProperty;
@@ -70,7 +71,7 @@ private void loadAssociation(Field field, FetchType fetchType, Class<?> annotati
7071

7172
ids = ((BasicDBObject) source).get(name);
7273

73-
if (DocumentUtils.isLoaded(((BasicDBObject) source).get(field.getName())) || ids == null) {
74+
if (DocumentUtils.isLoaded(((BasicDBObject) source).get(field.getName())) || ids == null || ((DBObject)ids).keySet().isEmpty()) {
7475
return;
7576
}
7677

src/main/java/io/github/kaiso/relmongo/lazy/RelMongoLazyLoader.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ public RelMongoLazyLoader(List<Object> ids, MongoOperations mongoOperations, Cla
2828

2929
@Override
3030
public Object loadObject() throws Exception {
31+
Object result = null;
3132
if (!(original instanceof LazyLoadingProxy) && !ids.isEmpty()) {
3233
if (Collection.class.isAssignableFrom(fieldType)) {
33-
return DatabaseOperations.findByIds(mongoOperations, targetClass, ids.toArray(new ObjectId[ids.size()]));
34+
result = DatabaseOperations.findByIds(mongoOperations, targetClass, ids.toArray(new ObjectId[ids.size()]));
3435
} else {
35-
return DatabaseOperations.findByPropertyValue(mongoOperations, targetClass, "_id", ids.get(0));
36+
result = DatabaseOperations.findByPropertyValue(mongoOperations, targetClass, "_id", ids.get(0));
3637
}
3738
}
38-
return original;
39+
return result != null ? result : original;
3940
}
4041

4142
}

src/test/java/io/github/kaiso/relmongo/data/model/Person.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,75 +9,85 @@
99
import org.bson.types.ObjectId;
1010
import org.springframework.data.mongodb.core.mapping.Document;
1111

12+
import java.util.ArrayList;
1213
import java.util.List;
1314

1415
@Document(collection = "people")
1516
public class Person {
1617
private ObjectId id;
1718
private String name;
1819
private String email;
19-
20-
@OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
21-
@JoinProperty(name="carsrefs")
20+
21+
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
22+
@JoinProperty(name = "carsrefs")
2223
private List<Car> cars;
23-
24-
@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.PERSIST)
25-
@JoinProperty(name="housesrefs")
26-
private List<House> houses;
27-
28-
@OneToOne(fetch=FetchType.EAGER)
24+
25+
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
26+
@JoinProperty(name = "housesrefs")
27+
private List<House> houses = new ArrayList<>();
28+
29+
@OneToOne(fetch = FetchType.EAGER)
2930
@JoinProperty(name = "passportref")
3031
private Passport passport;
31-
32-
@OneToOne(fetch=FetchType.LAZY, cascade = CascadeType.PERSIST)
32+
33+
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
3334
@JoinProperty(name = "drivingLicenseref")
3435
private DrivingLicense drivingLicense;
35-
36+
3637
public String getName() {
3738
return name;
3839
}
40+
3941
public void setName(String name) {
4042
this.name = name;
4143
}
44+
4245
public String getEmail() {
4346
return email;
4447
}
48+
4549
public void setEmail(String email) {
4650
this.email = email;
4751
}
52+
4853
public List<Car> getCars() {
4954
return cars;
5055
}
56+
5157
public void setCars(List<Car> cars) {
5258
this.cars = cars;
5359
}
60+
5461
public ObjectId getId() {
5562
return id;
5663
}
64+
5765
public void setId(ObjectId id) {
5866
this.id = id;
5967
}
68+
6069
public List<House> getHouses() {
6170
return houses;
6271
}
72+
6373
public void setHouses(List<House> houses) {
6474
this.houses = houses;
6575
}
76+
6677
public Passport getPassport() {
6778
return passport;
6879
}
80+
6981
public void setPassport(Passport passport) {
7082
this.passport = passport;
7183
}
84+
7285
public DrivingLicense getDrivingLicense() {
7386
return drivingLicense;
7487
}
88+
7589
public void setDrivingLicense(DrivingLicense drivingLicense) {
7690
this.drivingLicense = drivingLicense;
7791
}
78-
79-
8092

81-
82-
8393
}

0 commit comments

Comments
 (0)