@Bean public SessionFactory getSessionFactory() { // we need to specify which packages Neo4j should scan // we'll use classes in each package to avoid brittleness Class<?>[] packageClasses = { ProductRepository.class, ShipmentRepository.class, WarehouseRepository.class, AddressRepository.class, CatalogRepository.class, InventoryRepository.class }; String[] packageNames = Arrays.asList(packageClasses) .stream() .map( c -> getClass().getPackage().getName()) .collect(Collectors.toList()) .toArray(new String[packageClasses.length]); return new SessionFactory(packageNames); }
private void delete(List<? extends T> items) { Session session = this.sessionFactory.openSession(); for(T item : items) { session.delete(item); } }
/** * Creates a new {@link Neo4jAuditingEventListener} using the given {@link MappingContext} and {@link AuditingHandler} * provided by the given {@link ObjectFactory}. This constructor does an additional registration to the * {@link SessionFactory}. Therefore the {@link SessionFactory} must already be instantiated. * * @deprecated this constructor could create a cyclic dependency to the session factory if this class gets registered * as a bean in the context. * @param auditingHandlerFactory must not be {@literal null}. */ @Deprecated public Neo4jAuditingEventListener(ObjectFactory<? extends IsNewAwareAuditingHandler> auditingHandlerFactory, SessionFactory sessionFactory) { Assert.notNull(auditingHandlerFactory, "IsNewAwareAuditingHandler must not be null!"); this.auditingHandlerFactory = auditingHandlerFactory; sessionFactory.register(this); }
@Before public void init() throws IOException { session = sessionFactory.openSession(); session.purgeDatabase(); }
@Before public void init() throws IOException { session = new SessionFactory(driver, "org.neo4j.ogm.domain.generic_hierarchy.relationship").openSession(); session.purgeDatabase(); }
@BeforeClass public static void oneTimeSetUp() { sessionFactory = new SessionFactory(driver, "org.neo4j.ogm.domain.cineasts.annotated", "org.neo4j.ogm.domain.canonical.hierarchies"); }
@Test public void loadUsesIdWhenPresentOnParent() { ValidAnnotations.BasicChild entity = new ValidAnnotations.BasicChild(); entity.identifier = "id1"; session.save(entity); final Session session2 = sessionFactory.openSession(); final ValidAnnotations.Basic retrievedEntity = session2.load(ValidAnnotations.Basic.class, "id1"); assertThat(retrievedEntity).isNotNull(); assertThat(retrievedEntity.identifier).isEqualTo(entity.identifier); }
@BeforeClass public static void init() throws IOException { session = new SessionFactory(driver, UserWithEnumMap.class.getName()) .openSession(); }
@BeforeClass public static void oneTimeSetUp() { sessionFactory = new SessionFactory(driver, "org.neo4j.ogm.domain.filesystem", "org.neo4j.ogm.domain.cineasts.annotated"); eventListener = new TestEventListener(); sessionFactory.register(eventListener); }
@Test public void loadUsesGraphIdWhenPrimaryIndexNotPresent() { SessionFactory sessionFactory = new SessionFactory(driver, "org.neo4j.ogm.domain.cineasts.partial"); Session session1 = sessionFactory.openSession(); Actor actor = new Actor("David Hasslehoff"); session1.save(actor); final Long id = actor.getId(); final Session session2 = sessionFactory.openSession(); final Actor retrievedActor = session2.load(Actor.class, id); assertThat(retrievedActor).isNotNull(); assertThat(retrievedActor.getName()).isEqualTo(actor.getName()); }
@Test public void shouldLoadHttpDriver() { Configuration driverConfiguration = new Configuration.Builder().uri("http://neo4j:password@localhost:7474") .build(); SessionFactory sf = new SessionFactory(driverConfiguration, "org.neo4j.ogm.domain.social.User"); Driver driver = sf.unwrap(Driver.class); assertThat(driver).isNotNull(); sf.close(); }
@Test public void testAuthorizedDriver() { session = new SessionFactory(driver, "dummy").openSession(); try (Transaction ignored = session.beginTransaction()) { assertThat(ignored).isNotNull(); } catch (Exception rpe) { fail("'" + rpe.getLocalizedMessage() + "' was not expected here"); } }
@Test public void shouldMergeBasePackages() { Configuration configuration = getBaseConfiguration() .withBasePackages(Bike.class.getPackage().getName()) .build(); SessionFactory sessionFactory = new SessionFactory(configuration, Author.class.getPackage().getName()); assertThat(sessionFactory.metaData().classInfo(Bike.class)).isNotNull(); assertThat(sessionFactory.metaData().classInfo(Author.class)).isNotNull(); assertThat(sessionFactory.metaData().classInfo(Pizza.class)).isNull(); }
@Test public void testAutoIndexDumpCreatesIndex() throws IOException { File file = File.createTempFile("test", ".cql"); file.deleteOnExit(); Configuration configuration = getBaseConfiguration() .autoIndex("dump") .generatedIndexesOutputDir(file.getParent()) .generatedIndexesOutputFilename(file.getName()) .build(); sessionFactory.runAutoIndexManager(configuration); assertThat(file).exists().canRead(); assertThat(contentOf(file)) .contains(Arrays.stream(expectedIndexDefinitions).map(d -> "CREATE " + d).toArray(String[]::new)); }
@Test public void saveWithContextIdStrategy() throws Exception { CustomInstanceIdStrategy strategy = new CustomInstanceIdStrategy("test-custom-instance-id"); sessionFactory.register(strategy); ValidAnnotations.WithCustomInstanceIdStrategy entity = new ValidAnnotations.WithCustomInstanceIdStrategy(); session.save(entity); assertThat(entity.identifier).isEqualTo("test-custom-instance-id"); session.clear(); ValidAnnotations.WithCustomInstanceIdStrategy loaded = session .load(ValidAnnotations.WithCustomInstanceIdStrategy.class, "test-custom-instance-id"); assertThat(loaded).isNotNull(); assertThat(loaded.identifier).isEqualTo("test-custom-instance-id"); }
@Test // GH-385 public void shouldLoadSimpleRelationshipIntoCollection() { final GraphDatabaseService database = getGraphDatabaseService(); long id = (long) database.execute("" + "CREATE (p:P {name: 'P'})\n" + "MERGE (p) - [:HAS] -> (:C {name: 'C1'})\n" + "MERGE (p) - [:HAS] -> (:C {name: 'C2'})\n" + "RETURN id(p) AS id" ).next().get("id"); Session session = sessionFactory.openSession(); P p = session.load(P.class, id); assertThat(p) .isNotNull() .extracting(P::getC).asList() .hasSize(2); }
@Test public void indexesShouldBeCreatedForLoadableClassesInHierarchy() { 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(getBaseConfiguration().autoIndex(AutoIndexMode.UPDATE.name()).build()); GraphDatabaseService service = getGraphDatabaseService(); try (Transaction tx = service.beginTx()) { IndexDescription[] indexes = StreamSupport.stream(service.schema().getIndexes().spliterator(), false) .map(IndexDescription::new).toArray(IndexDescription[]::new); assertThat(indexes).containsExactlyInAnyOrder(expectedIndexes); tx.success(); } }
@Test // GH-479 public void shouldIgnoreResultsIfQueryMapsToVoid() { SessionFactory sessionFactory = null; try (Driver driver = GraphDatabase.driver(boltURI, driverConfig)) { sessionFactory = new SessionFactory(new BoltDriver(driver), ResponseMapperTest.class.getName()); Session session = sessionFactory.openSession(); // The call would have been successfull if the procedure would have returned a stream of // things with at max 1 attribute, but in the end, that would be what was requested for. session.query(void.class, "CALL apoc.periodic.iterate('MATCH (d:Document) RETURN d', " + " 'SET d.thisIsAProperty = 0'" + " ,{batchSize:200, parallel:false, iterateList:true}) ", new HashMap<>()); } finally { if (sessionFactory != null) { sessionFactory.close(); } } }
@Test(expected = Exception.class) public void shouldThrowExceptionWhenEmbeddedDriverCannotConnect() { Configuration configuration = new Configuration.Builder( new ClasspathConfigurationSource("ogm-embedded-invalid.properties")).build(); SessionFactory sessionFactory = new SessionFactory(configuration, "org.neo4j.ogm.domain.social"); Session session = sessionFactory.openSession(); session.purgeDatabase(); }
.trustStrategy("ACCEPT_UNSIGNED").build(); SessionFactory sf = new SessionFactory(configuration, "org.neo4j.ogm.domain.social.User"); try (HttpDriver driver = sf.unwrap(HttpDriver.class)) { driver.configure(configuration); driver.executeHttpRequest(request); sf.close(); sf = new SessionFactory(configuration, "org.neo4j.ogm.domain.social.User"); try (HttpDriver driver = sf.unwrap(HttpDriver.class);) { driver.configure(configuration); driver.executeHttpRequest(request); Assert.fail("Should NOT have thrown security exception"); sf.close();