use of org.neo4j.ogm.transaction.Transaction in project neo4j-ogm by neo4j.
the class LabelDeterminationTest method indexesShouldBeCreatedForLoadableClassesInHierarchy.
@Test
public void indexesShouldBeCreatedForLoadableClassesInHierarchy() {
assumeTrue(isVersionOrGreater("3.3"));
final IndexDescription[] expectedIndexes = new IndexDescription[] { new IndexDescription("DefaultUser", "id"), new IndexDescription("Admin", "id"), new IndexDescription("ChildA", "uuid"), new IndexDescription("ChildB", "uuid"), new IndexDescription("ChildC", "uuid"), new IndexDescription("LabeledEntity", "uuid"), new IndexDescription("EntityWithImplicitPlusAdditionalLabels", "id") };
sessionFactory.runAutoIndexManager(getBaseConfigurationBuilder().autoIndex(AutoIndexMode.UPDATE.name()).build());
Session session = sessionFactory.openSession();
try (Transaction tx = session.beginTransaction()) {
Result indexResult = session.query("CALL db.indexes()", emptyMap());
List<IndexDescription> indexDescriptions = new ArrayList<>();
for (Map<String, Object> queryResult : indexResult.queryResults()) {
// Skip 4.3 node label / rel types
if (queryResult.containsKey("type") && queryResult.get("type").equals("LOOKUP")) {
continue;
}
// ensure compatibility with 3.x and 4.x
String label = "unknown";
if (queryResult.get("label") != null) {
// 3.4(-)
label = (String) queryResult.get("label");
} else if (queryResult.get("tokenNames") != null) {
// 3.5
label = ((String[]) queryResult.get("tokenNames"))[0];
} else if (queryResult.get("labelsOrTypes") != null) {
// 4.0+
label = ((String[]) queryResult.get("labelsOrTypes"))[0];
}
indexDescriptions.add(new IndexDescription(label, (String[]) queryResult.get("properties")));
}
assertThat(indexDescriptions).containsExactlyInAnyOrder(expectedIndexes);
tx.commit();
}
}
use of org.neo4j.ogm.transaction.Transaction in project neo4j-ogm by neo4j.
the class SatelliteIntegrationTest method shouldRollbackLongTransaction.
@Test
public void shouldRollbackLongTransaction() {
Long id;
String name;
try (Transaction tx = session.beginTransaction()) {
// load all
Collection<Satellite> satellites = session.loadAll(Satellite.class);
assertThat(satellites).hasSize(11);
Satellite satellite = satellites.iterator().next();
id = satellite.getId();
name = satellite.getName();
satellite.setName("Updated satellite");
// update
session.save(satellite);
session.clear();
// refetch
Satellite updatedSatellite = session.load(Satellite.class, id);
assertThat(updatedSatellite.getName()).isEqualTo("Updated satellite");
tx.rollback();
}
session.clear();
// fetch - after rollback should not be changed
// note, that because we aren't starting a new tx, we will be given an autocommit one.
Satellite reloadedSatellite = session.load(Satellite.class, id);
assertThat(reloadedSatellite.getName()).isEqualTo(name);
}
use of org.neo4j.ogm.transaction.Transaction in project neo4j-ogm by neo4j.
the class HierarchyRelsTest method saveMultipleRelationshipsToBase.
// GH-152
@Test
public void saveMultipleRelationshipsToBase() {
Type1 node1 = new Type1();
node1.name = "type1";
Type2 node2 = new Type2();
node2.name = "type2";
node1.addIncoming(node2);
node2.addIncoming(node1);
Transaction transaction = session.beginTransaction();
session.save(node1);
transaction.commit();
transaction.close();
session.clear();
transaction = session.beginTransaction();
BaseEntity entity = session.load(BaseEntity.class, node1.getGraphId());
transaction.close();
assertThat(entity.getIncoming()).hasSize(1);
assertThat(entity.getOutgoing()).hasSize(1);
assertThat(node2.getGraphId()).isEqualTo(entity.getIncoming().get(0).getGraphId());
assertThat(node2.getGraphId()).isEqualTo(entity.getOutgoing().get(0).getGraphId());
}
use of org.neo4j.ogm.transaction.Transaction in project neo4j-ogm by neo4j.
the class SessionAndMappingContextTest method shouldRollbackRelationshipEntityWithDifferentStartAndEndNodes.
@Test
public void shouldRollbackRelationshipEntityWithDifferentStartAndEndNodes() {
Actor mary = new Actor("Mary");
Actor john = new Actor("John");
Knows maryKnowsJohn = new Knows();
maryKnowsJohn.setFirstActor(mary);
maryKnowsJohn.setSecondActor(john);
try (Transaction tx = session.beginTransaction()) {
session.save(maryKnowsJohn);
assertThat(mary.getUuid()).isNotNull();
assertThat(maryKnowsJohn.id).isNotNull();
assertThat(john.getUuid()).isNotNull();
tx.rollback();
assertThat(mary.getUuid()).isNull();
assertThat(maryKnowsJohn.id).isNull();
assertThat(john.getUuid()).isNull();
}
}
use of org.neo4j.ogm.transaction.Transaction in project neo4j-ogm by neo4j.
the class SessionAndMappingContextTest method shouldRefreshUpdatedRelationshipEntities.
// GH-817
@Test
public void shouldRefreshUpdatedRelationshipEntities() {
sessionFactory.openSession().purgeDatabase();
Session sessionForCreation = sessionFactory.openSession();
Rider rider = new Rider("Michael");
Bike bike1 = new Bike("Bike1");
Bike bike2 = new Bike("Bike2");
rider.getTrips().add(new Trip(rider, bike1, "n/a"));
rider.getTrips().add(new Trip(rider, bike2, "n/a"));
try (Transaction tx = sessionForCreation.beginTransaction()) {
sessionForCreation.save(rider);
tx.commit();
}
Session sessionForLoadingAndUpdate = sessionFactory.openSession();
Iterable<Trip> loadedBikes = sessionForLoadingAndUpdate.loadAll(Trip.class);
assertThat(loadedBikes).hasSize(2).extracting(Trip::getName).containsOnly("n/a");
String name = "A nice trip";
Iterable<Trip> updatedBikes = sessionForLoadingAndUpdate.query(Trip.class, "MATCH (r:Rider) - [t:RODE] -> (c:Bike) SET t.name = $newName RETURN *", Collections.singletonMap("newName", name));
Rider loadedRider = sessionForLoadingAndUpdate.load(Rider.class, rider.getId());
assertThat(updatedBikes).hasSize(2).extracting(Trip::getName).containsOnly(name);
assertThat(loadedRider.getTrips()).hasSize(2).extracting(Trip::getName).containsOnly(name);
}
Aggregations