Commit b183c0b7 authored by Andy Wilkinson's avatar Andy Wilkinson

Merge pull request #15637 from Michael Simons

* gh-15637:
  Polish "Add opt-in support for Neo4j-OGM native types"
  Add opt-in support for Neo4j-OGM native types

Closes gh-15637
parents c8070f44 18174a8e
...@@ -782,7 +782,7 @@ ...@@ -782,7 +782,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.neo4j</groupId> <groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-http-driver</artifactId> <artifactId>neo4j-ogm-bolt-native-types</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
...@@ -790,6 +790,11 @@ ...@@ -790,6 +790,11 @@
<artifactId>neo4j-ogm-embedded-driver</artifactId> <artifactId>neo4j-ogm-embedded-driver</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-http-driver</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -33,6 +33,7 @@ import org.springframework.util.ClassUtils; ...@@ -33,6 +33,7 @@ import org.springframework.util.ClassUtils;
* @author Michael Hunger * @author Michael Hunger
* @author Vince Bickers * @author Vince Bickers
* @author Aurélien Leboulanger * @author Aurélien Leboulanger
* @author Michael Simons
* @since 1.4.0 * @since 1.4.0
*/ */
@ConfigurationProperties(prefix = "spring.data.neo4j") @ConfigurationProperties(prefix = "spring.data.neo4j")
...@@ -72,6 +73,11 @@ public class Neo4jProperties implements ApplicationContextAware { ...@@ -72,6 +73,11 @@ public class Neo4jProperties implements ApplicationContextAware {
*/ */
private Boolean openInView; private Boolean openInView;
/**
* Whether to use Neo4j native types wherever possible.
*/
private boolean useNativeTypes = false;
private final Embedded embedded = new Embedded(); private final Embedded embedded = new Embedded();
private ClassLoader classLoader = Neo4jProperties.class.getClassLoader(); private ClassLoader classLoader = Neo4jProperties.class.getClassLoader();
...@@ -116,6 +122,14 @@ public class Neo4jProperties implements ApplicationContextAware { ...@@ -116,6 +122,14 @@ public class Neo4jProperties implements ApplicationContextAware {
this.openInView = openInView; this.openInView = openInView;
} }
public boolean isUseNativeTypes() {
return this.useNativeTypes;
}
public void setUseNativeTypes(boolean useNativeTypes) {
this.useNativeTypes = useNativeTypes;
}
public Embedded getEmbedded() { public Embedded getEmbedded() {
return this.embedded; return this.embedded;
} }
...@@ -146,6 +160,9 @@ public class Neo4jProperties implements ApplicationContextAware { ...@@ -146,6 +160,9 @@ public class Neo4jProperties implements ApplicationContextAware {
builder.credentials(this.username, this.password); builder.credentials(this.username, this.password);
} }
builder.autoIndex(this.getAutoIndex().getName()); builder.autoIndex(this.getAutoIndex().getName());
if (this.useNativeTypes) {
builder.useNativeTypes();
}
} }
private void configureUriWithDefaults(Builder builder) { private void configureUriWithDefaults(Builder builder) {
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.data.neo4j; ...@@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.data.neo4j;
import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Caffeine;
import org.junit.Test; import org.junit.Test;
import org.neo4j.ogm.driver.NativeTypesNotAvailableException;
import org.neo4j.ogm.driver.NativeTypesNotSupportedException;
import org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver; import org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver;
import org.neo4j.ogm.session.Session; import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory; import org.neo4j.ogm.session.SessionFactory;
...@@ -51,8 +53,8 @@ import static org.mockito.Mockito.mock; ...@@ -51,8 +53,8 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link Neo4jDataAutoConfiguration}. Tests can't use the embedded driver as we * Tests for {@link Neo4jDataAutoConfiguration}. Tests should not use the embedded driver
* use Lucene 4 and Neo4j still requires 3. * as it requires the complete Neo4j-Kernel and server to function properly.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Michael Hunger * @author Michael Hunger
...@@ -116,7 +118,6 @@ public class Neo4jDataAutoConfigurationTests { ...@@ -116,7 +118,6 @@ public class Neo4jDataAutoConfigurationTests {
assertThat(context) assertThat(context)
.hasSingleBean(org.neo4j.ogm.config.Configuration.class); .hasSingleBean(org.neo4j.ogm.config.Configuration.class);
}); });
} }
@Test @Test
...@@ -144,6 +145,48 @@ public class Neo4jDataAutoConfigurationTests { ...@@ -144,6 +145,48 @@ public class Neo4jDataAutoConfigurationTests {
.doesNotHaveBean(OpenSessionInViewInterceptor.class)); .doesNotHaveBean(OpenSessionInViewInterceptor.class));
} }
@Test
public void shouldBeAbleToUseNativeTypesWithBolt() {
this.contextRunner
.withPropertyValues("spring.data.neo4j.uri=bolt://localhost:7687",
"spring.data.neo4j.use-native-types:true")
.withConfiguration(AutoConfigurations.of(Neo4jDataAutoConfiguration.class,
TransactionAutoConfiguration.class))
.run((context) -> assertThat(context)
.getBean(org.neo4j.ogm.config.Configuration.class)
.hasFieldOrPropertyWithValue("useNativeTypes", true));
}
@Test
public void shouldFailWhenNativeTypesAreNotAvailable() {
this.contextRunner
.withClassLoader(
new FilteredClassLoader("org.neo4j.ogm.drivers.bolt.types"))
.withPropertyValues("spring.data.neo4j.uri=bolt://localhost:7687",
"spring.data.neo4j.use-native-types:true")
.withConfiguration(AutoConfigurations.of(Neo4jDataAutoConfiguration.class,
TransactionAutoConfiguration.class))
.run((context) -> {
assertThat(context).hasFailed();
assertThat(context.getStartupFailure()).hasRootCauseInstanceOf(
NativeTypesNotAvailableException.class);
});
}
@Test
public void shouldFailWhenNativeTypesAreNotSupported() {
this.contextRunner
.withPropertyValues("spring.data.neo4j.uri=http://localhost:7474",
"spring.data.neo4j.use-native-types:true")
.withConfiguration(AutoConfigurations.of(Neo4jDataAutoConfiguration.class,
TransactionAutoConfiguration.class))
.run((context) -> {
assertThat(context).hasFailed();
assertThat(context.getStartupFailure()).hasRootCauseInstanceOf(
NativeTypesNotSupportedException.class);
});
}
@Test @Test
public void eventListenersAreAutoRegistered() { public void eventListenersAreAutoRegistered() {
this.contextRunner.withUserConfiguration(EventListenerConfiguration.class) this.contextRunner.withUserConfiguration(EventListenerConfiguration.class)
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link Neo4jProperties}. * Tests for {@link Neo4jProperties}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Michael Simons
*/ */
public class Neo4jPropertiesTests { public class Neo4jPropertiesTests {
...@@ -147,6 +148,21 @@ public class Neo4jPropertiesTests { ...@@ -147,6 +148,21 @@ public class Neo4jPropertiesTests {
"file:relative/path/to/my.db"); "file:relative/path/to/my.db");
} }
@Test
public void nativeTypesAreSetToFalseByDefault() {
Neo4jProperties properties = load(true);
Configuration configuration = properties.createConfiguration();
assertThat(configuration.getUseNativeTypes()).isFalse();
}
@Test
public void nativeTypesCanBeConfigured() {
Neo4jProperties properties = load(true,
"spring.data.neo4j.use-native-types=true");
Configuration configuration = properties.createConfiguration();
assertThat(configuration.getUseNativeTypes()).isTrue();
}
private static void assertDriver(Configuration actual, String driver, String uri) { private static void assertDriver(Configuration actual, String driver, String uri) {
assertThat(actual).isNotNull(); assertThat(actual).isNotNull();
assertThat(actual.getDriverClassName()).isEqualTo(driver); assertThat(actual.getDriverClassName()).isEqualTo(driver);
......
...@@ -2527,6 +2527,11 @@ ...@@ -2527,6 +2527,11 @@
<artifactId>neo4j-ogm-bolt-driver</artifactId> <artifactId>neo4j-ogm-bolt-driver</artifactId>
<version>${neo4j-ogm.version}</version> <version>${neo4j-ogm.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-bolt-native-types</artifactId>
<version>${neo4j-ogm.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.neo4j</groupId> <groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-core</artifactId> <artifactId>neo4j-ogm-core</artifactId>
...@@ -2537,6 +2542,11 @@ ...@@ -2537,6 +2542,11 @@
<artifactId>neo4j-ogm-embedded-driver</artifactId> <artifactId>neo4j-ogm-embedded-driver</artifactId>
<version>${neo4j-ogm.version}</version> <version>${neo4j-ogm.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-embedded-native-types</artifactId>
<version>${neo4j-ogm.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.neo4j</groupId> <groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-http-driver</artifactId> <artifactId>neo4j-ogm-http-driver</artifactId>
......
...@@ -695,6 +695,7 @@ content into your application. Rather, pick only the properties that you need. ...@@ -695,6 +695,7 @@ content into your application. Rather, pick only the properties that you need.
spring.data.neo4j.password= # Login password of the server. spring.data.neo4j.password= # Login password of the server.
spring.data.neo4j.repositories.enabled=true # Whether to enable Neo4j repositories. spring.data.neo4j.repositories.enabled=true # Whether to enable Neo4j repositories.
spring.data.neo4j.uri= # URI used by the driver. Auto-detected by default. spring.data.neo4j.uri= # URI used by the driver. Auto-detected by default.
spring.data.neo4j.use-native-types=false # Whether to use Neo4j native types wherever possible.
spring.data.neo4j.username= # Login user of the server. spring.data.neo4j.username= # Login user of the server.
# DATA REST ({sc-spring-boot-autoconfigure}/data/rest/RepositoryRestProperties.{sc-ext}[RepositoryRestProperties]) # DATA REST ({sc-spring-boot-autoconfigure}/data/rest/RepositoryRestProperties.{sc-ext}[RepositoryRestProperties])
......
...@@ -4440,6 +4440,22 @@ in your configuration, e.g. `spring.data.neo4j.uri=file://var/tmp/graph.db`. ...@@ -4440,6 +4440,22 @@ in your configuration, e.g. `spring.data.neo4j.uri=file://var/tmp/graph.db`.
[[boot-features-neo4j-ogm-native-types]]
==== Using Native Types
Neo4j-OGM can map some types, like those in `java.time.*`, to `String`-based properties
or to one of the native types that Neo4j provides. For backwards compatibility reasons
the default for Neo4j-OGM is to use a `String`-based representation. To use native types,
add a dependency on either `org.neo4j:neo4j-ogm-bolt-native-types` or
`org.neo4j:neo4j-ogm-embedded-native-types`, and configure the
`spring.data.neo4j.use-native-types` property as shown in the following example:
[source,properties,indent=0]
----
spring.data.neo4j.use-native-types=true
----
[[boot-features-neo4j-ogm-session]] [[boot-features-neo4j-ogm-session]]
==== Neo4jSession ==== Neo4jSession
By default, if you are running a web application, the session is bound to the thread for By default, if you are running a web application, the session is bound to the thread for
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment