Search in sources :

Example 1 with Transaction

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();
    }
}
Also used : Transaction(org.neo4j.ogm.transaction.Transaction) ArrayList(java.util.ArrayList) Session(org.neo4j.ogm.session.Session) Result(org.neo4j.ogm.model.Result) Test(org.junit.Test)

Example 2 with Transaction

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);
}
Also used : Transaction(org.neo4j.ogm.transaction.Transaction) Satellite(org.neo4j.ogm.domain.satellites.Satellite) Test(org.junit.Test)

Example 3 with Transaction

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());
}
Also used : Type2(org.neo4j.ogm.domain.hierarchy.relations.Type2) Transaction(org.neo4j.ogm.transaction.Transaction) BaseEntity(org.neo4j.ogm.domain.hierarchy.relations.BaseEntity) Type1(org.neo4j.ogm.domain.hierarchy.relations.Type1) Test(org.junit.Test)

Example 4 with Transaction

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();
    }
}
Also used : Transaction(org.neo4j.ogm.transaction.Transaction) Actor(org.neo4j.ogm.domain.cineasts.annotated.Actor) Knows(org.neo4j.ogm.domain.cineasts.annotated.Knows) Test(org.junit.Test)

Example 5 with Transaction

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);
}
Also used : Trip(org.neo4j.ogm.domain.gh817.Trip) Transaction(org.neo4j.ogm.transaction.Transaction) Rider(org.neo4j.ogm.domain.gh817.Rider) Bike(org.neo4j.ogm.domain.gh817.Bike) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Session(org.neo4j.ogm.session.Session) Test(org.junit.Test)

Aggregations

Transaction (org.neo4j.ogm.transaction.Transaction)47 Test (org.junit.Test)30 Session (org.neo4j.ogm.session.Session)10 ArrayList (java.util.ArrayList)5 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)5 SessionConfig (org.neo4j.driver.SessionConfig)4 Result (org.neo4j.ogm.model.Result)4 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 HttpResponseException (org.apache.http.client.HttpResponseException)3 Bike (org.neo4j.ogm.domain.gh817.Bike)3 Studio (org.neo4j.ogm.domain.music.Studio)3 Satellite (org.neo4j.ogm.domain.satellites.Satellite)3 User (org.neo4j.ogm.domain.social.User)3 SessionFactory (org.neo4j.ogm.session.SessionFactory)3 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2