Skip to content

Commit 1846bc5

Browse files
Fixed some lazy repo problems
1 parent 474f339 commit 1846bc5

3 files changed

Lines changed: 20 additions & 13 deletions

File tree

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/EclipseStoreStorage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ public <T, ID> void createNewEntityData(final Class<T> entityClass, final Versio
212212
final SimpleEclipseStoreRepository<?, ?> repository = this.entityClassToRepository.get(entityClass);
213213
if(repository != null && repository.isLazy())
214214
{
215-
root.getCurrentRootData().createNewLazyEntityData(entityClass, idManager);
215+
root.getCurrentRootData().createNewLazyEntityData(entityClass, idManager.hasIdField() ? idManager : null);
216216
}
217217
else
218218
{
219-
root.getCurrentRootData().createNewEntityData(entityClass, idManager);
219+
root.getCurrentRootData().createNewEntityData(entityClass, idManager.hasIdField() ? idManager : null);
220220
}
221221
}
222222

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/root/v2_4/LazyEntityData.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Collection;
1919
import java.util.HashMap;
2020
import java.util.List;
21-
import java.util.function.Function;
2221
import java.util.stream.Stream;
2322

2423
import org.eclipse.serializer.reference.Lazy;
@@ -83,7 +82,8 @@ public boolean containsEntity(final T entity)
8382
else
8483
{
8584
final ID id = this.idGetter.getId(entity);
86-
return this.entitiesById.containsKey(id);
85+
final Lazy<T> existingEntity = this.entitiesById.get(id);
86+
return existingEntity != null && existingEntity.get() == entity;
8787
}
8888
}
8989

@@ -128,18 +128,25 @@ public void setLastId(final Object lastId)
128128
@Override
129129
public Collection<Object> ensureEntityAndReturnObjectsToStore(final T entityToStore)
130130
{
131-
if(!this.containsEntity(entityToStore))
131+
Collection<Object> listToSave = List.of();
132+
if(this.idGetter == null && !this.containsEntity(entityToStore))
132133
{
133-
final SpringDataEclipseStoreLazy.Default<T> newLazyEntity =
134-
SpringDataEclipseStoreLazy.build(entityToStore);
135-
this.entities.add(newLazyEntity);
136-
if(this.idGetter != null)
134+
this.entities.add(SpringDataEclipseStoreLazy.build(entityToStore));
135+
listToSave = this.getObjectsToStore();
136+
}
137+
if(this.idGetter != null)
138+
{
139+
final Lazy<T> existingEntity = this.entitiesById.get(this.idGetter.getId(entityToStore));
140+
if(existingEntity == null || existingEntity.get() != entityToStore)
137141
{
138-
this.entitiesById.put(this.idGetter.getId(entityToStore), newLazyEntity);
142+
this.entities.remove(existingEntity);
143+
final SpringDataEclipseStoreLazy<T> newLazyInstance = SpringDataEclipseStoreLazy.build(entityToStore);
144+
this.entities.add(newLazyInstance);
145+
this.entitiesById.put(this.idGetter.getId(entityToStore), newLazyInstance);
146+
listToSave = this.getObjectsToStore();
139147
}
140-
return this.getObjectsToStore();
141148
}
142-
return List.of();
149+
return listToSave;
143150
}
144151

145152
@Override

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/helper/DummyEntityProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public DummyEntityProvider(final Collection<T> collection)
2929
{
3030
super();
3131
final EntityData<T, Void> objects = new NonLazyEntityData<>();
32-
objects.setIdGetter(i -> null);
32+
objects.setIdGetter(null);
3333
collection.forEach(objects::ensureEntityAndReturnObjectsToStore);
3434
this.addEntityData(objects);
3535
}

0 commit comments

Comments
 (0)