Skip to content

Commit

Permalink
Convert array correct in delete operations.
Browse files Browse the repository at this point in the history
The array object is taken as an single object into a new list.
This causes operations to fail because they try to resolve e.g. the id
of an array object instead of the every single entry.

Fixes #509
  • Loading branch information
meistermeier committed Aug 22, 2018
1 parent aaf8a31 commit d825be1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.neo4j.ogm.session.delegates;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -59,7 +60,8 @@ private DeleteStatements getDeleteStatementsBasedOnType(Class type) {
private <T> void deleteAll(T object) {
List<T> list;
if (object.getClass().isArray()) {
list = Collections.singletonList(object);
T[] objectsAsArray = (T[]) object;
list = Arrays.asList(objectsAsArray);
} else {
list = (List<T>) object;
}
Expand Down Expand Up @@ -111,7 +113,7 @@ private void deleteOneOrMoreObjects(Set<Object> neighbours, List<?> objects) {
}
}
RowModelRequest query = new DefaultRowModelRequest(request.getStatement(), request.getParameters());
session.doInTransaction( () -> {
session.doInTransaction(() -> {
try (Response<RowModel> response = session.requestHandler().execute(query)) {

if (request.optimisticLockingConfig().isPresent()) {
Expand Down Expand Up @@ -153,7 +155,7 @@ private Statement getDeleteStatement(Object object, Long identity, ClassInfo cla
Statement request;
if (classInfo.hasVersionField()) {
request = deleteStatements.delete(identity, object, classInfo);
} else{
} else {
request = deleteStatements.delete(identity);
}
return request;
Expand All @@ -171,7 +173,7 @@ public <T> void deleteAll(Class<T> type) {
Statement request = getDeleteStatementsBasedOnType(type).delete(entityLabel);
RowModelRequest query = new DefaultRowModelRequest(request.getStatement(), request.getParameters());
session.notifyListeners(new PersistenceEvent(type, Event.TYPE.PRE_DELETE));
session.doInTransaction( () -> {
session.doInTransaction(() -> {
try (Response<RowModel> response = session.requestHandler().execute(query)) {
session.context().removeType(type);
if (session.eventsEnabled()) {
Expand Down Expand Up @@ -286,7 +288,7 @@ private void postDelete(Long identity, boolean isRelationshipEntity) {
public void purgeDatabase() {
Statement stmt = new NodeDeleteStatements().deleteAll();
RowModelRequest query = new DefaultRowModelRequest(stmt.getStatement(), stmt.getParameters());
session.doInTransaction( () -> {
session.doInTransaction(() -> {
session.requestHandler().execute(query).close();
}, Transaction.Type.READ_WRITE);
session.context().clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

package org.neo4j.ogm.persistence.session.capability;

import static org.assertj.core.api.Assertions.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -51,4 +56,60 @@ public void shouldNotFailIfDeleteNodeEntityAgainstEmptyDatabase() {
public void shouldNotFailIfDeleteRelationshipEntityAgainstEmptyDatabase() {
session.deleteAll(Recording.class);
}

@Test
public void canDeleteSingleEntry() {
Album album = new Album();
session.save(album);
session.clear();
assertEntityCount(1);

session.delete(album);
assertEntityCount(0);
}

@Test
public void canDeleteEntityCollection() {
Album album1 = new Album();
Album album2 = new Album();
session.save(album1);
session.save(album2);
session.clear();
assertEntityCount(2);

List<Object> albumList = new ArrayList<>();
albumList.add(album1);
albumList.add(album2);

session.delete(albumList);
assertEntityCount(0);
}

// gh-#509
@Test
public void canDeleteEntityArray() {
Album album1 = new Album();
Album album2 = new Album();
session.save(album1);
session.save(album2);
session.clear();

assertEntityCount(2);

List<Object> albumList = new ArrayList<>();
albumList.add(album1);
albumList.add(album2);

session.delete(albumList.toArray());

assertEntityCount(0);

}

private void assertEntityCount(int count) {
session.clear();
long entityCount = session.countEntitiesOfType(Album.class);
assertThat(entityCount).isEqualTo(count);
session.clear();
}
}

0 comments on commit d825be1

Please sign in to comment.