use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.
the class SaveCapabilityTest method shouldSaveOnlyModifiedNodes.
@Test
public void shouldSaveOnlyModifiedNodes() {
int depth = 1;
Neo4jSession neo4jSession = (Neo4jSession) session;
CompileContext context = null;
Artist leann = new Artist("Leann Rimes");
Album lost = new Album("Lost Highway");
lost.setArtist(bonJovi);
lost.setGuestArtist(leann);
context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(lost, depth);
assertThat(context.registry()).as("Should save 3 nodes and 2 relations (5 items)").hasSize(5);
session.save(lost);
context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(lost, depth);
assertThat(context.registry()).as("Should have nothing to save").isEmpty();
session.clear();
Artist loadedLeann = session.load(Artist.class, leann.getId(), depth);
context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(loadedLeann, depth);
assertThat(context.registry()).as("Should have nothing to save").isEmpty();
loadedLeann.setName("New name");
context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(loadedLeann, depth);
assertThat(context.registry()).as("Should have one node to save").hasSize(1);
loadedLeann.getGuestAlbums().iterator().next().setName("New Album Name");
context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(loadedLeann, depth);
assertThat(context.registry()).as("Should have two node to save").hasSize(2);
}
use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.
the class LoadCapabilityTest method shouldNotBeDirtyOnLoadEntityThenSaveThenReload.
// GH-177
@Test
public void shouldNotBeDirtyOnLoadEntityThenSaveThenReload() {
MappingContext context = ((Neo4jSession) session).context();
Artist pinkFloyd = new Artist("Pink Floyd");
// new object not saved is always dirty
assertThat(context.isDirty(pinkFloyd)).isTrue();
session.save(pinkFloyd);
// object hash updated by save.
assertThat(context.isDirty(pinkFloyd)).isFalse();
// forget everything we've done
session.clear();
pinkFloyd = session.load(Artist.class, pinkFloyd.getId());
// a freshly loaded object is never dirty
assertThat(context.isDirty(pinkFloyd)).isFalse();
pinkFloyd.setName("Purple Floyd");
// we changed the name so it is now dirty
assertThat(context.isDirty(pinkFloyd)).isTrue();
session.save(pinkFloyd);
// object saved, no longer dirty
assertThat(context.isDirty(pinkFloyd)).isFalse();
// load the same identity, but to a copy ref
Artist purpleFloyd = session.load(Artist.class, pinkFloyd.getId());
// nothing has changed, so it should not be dirty
assertThat(context.isDirty(purpleFloyd)).isFalse();
// two refs pointing to the same object
assertThat(pinkFloyd == purpleFloyd).isTrue();
}
use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.
the class LoadCapabilityTest method shouldNotBeDirtyOnLoadRelationshipEntityThenSaveThenReload.
// GH-177
@Test
public void shouldNotBeDirtyOnLoadRelationshipEntityThenSaveThenReload() {
MappingContext context = ((Neo4jSession) session).context();
Artist pinkFloyd = new Artist("Pink Floyd");
Album divisionBell = new Album("The Division Bell");
divisionBell.setArtist(pinkFloyd);
Studio studio = new Studio("Britannia Row Studios");
Recording recording = new Recording(divisionBell, studio, 1994);
divisionBell.setRecording(recording);
pinkFloyd.addAlbum(divisionBell);
// new object not saved is always dirty
assertThat(context.isDirty(recording)).isTrue();
session.save(recording);
// object hash updated by save.
assertThat(context.isDirty(recording)).isFalse();
// forget everything we've done
session.clear();
recording = session.load(Recording.class, recording.getId(), 2);
// a freshly loaded object is never dirty
assertThat(context.isDirty(recording)).isFalse();
recording.setYear(1995);
// we changed the year so it is now dirty
assertThat(context.isDirty(recording)).isTrue();
session.save(recording);
// object saved, no longer dirty
assertThat(context.isDirty(recording)).isFalse();
Recording recording1995 = session.load(Recording.class, recording.getId(), // load the same identity, but to a copy ref
2);
// nothing has changed, so it should not be dirty
assertThat(context.isDirty(recording1995)).isFalse();
// two refs pointing to the same object
assertThat(recording == recording1995).isTrue();
}
use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.
the class BasicDriverTest method customWriteProtectionStrategyShouldBeApplied.
@Test
public void customWriteProtectionStrategyShouldBeApplied() {
Predicate<Object> alwaysWriteProtect = o -> true;
Predicate<Object> protectAvon = o -> ((User) o).getName().startsWith("Avon");
WriteProtectionStrategy customStrategy = () -> (target, clazz) -> {
// You than can omit the class check in the predicate
if (clazz == Immortal.class) {
return alwaysWriteProtect;
} else if (clazz == User.class) {
return protectAvon;
} else {
// Person.class in the example
return null;
}
};
User avon = new User("Avon Barksdale");
session.save(avon);
User stringer = new User("Stringer Bell");
session.save(stringer);
Immortal connor = new Immortal("Connor", "MacLeod");
session.save(connor);
Person person = new Person("A person");
session.save(person);
session.clear();
try {
((Neo4jSession) session).setWriteProtectionStrategy(customStrategy);
stringer.setName("Avon Something");
avon.befriend(stringer);
session.save(avon);
connor.setFirstName("Duncan");
session.save(connor);
person.setName("John Reese");
session.save(person);
session.clear();
Collection<User> users = session.loadAll(User.class);
assertThat(users).hasSize(2).extracting(User::getName).containsExactlyInAnyOrder("Avon Barksdale", "Stringer Bell");
Collection<Immortal> immortals = session.loadAll(Immortal.class);
assertThat(immortals).hasSize(1).extracting(Immortal::getFirstName).containsExactlyInAnyOrder("Connor");
Collection<Person> personsOfInterest = session.loadAll(Person.class);
assertThat(personsOfInterest).hasSize(1).extracting(Person::getName).containsExactlyInAnyOrder("John Reese");
} finally {
((Neo4jSession) session).setWriteProtectionStrategy(null);
}
}
Aggregations