更新: 2018-04-21
GraphAware 的UUID自動生成方式在結合Spring Data Neo4j使用的狀況下, 當經過Neo4jRepository.save()
建立節點的時候, 沒法返回節點的UUID. 由於其UUID是做爲事務回調(異步)中生成的, 沒法同步返回.該方式已經被Spring Data Neo4j 5.0的ID生成策略(UuidStrategy)替代.java
import lombok.Getter; import lombok.NonNull; import lombok.Setter; import org.neo4j.ogm.annotation.GeneratedValue; import org.neo4j.ogm.annotation.Id; import org.neo4j.ogm.annotation.NodeEntity; import org.neo4j.ogm.annotation.typeconversion.Convert; import org.neo4j.ogm.id.UuidStrategy; import org.neo4j.ogm.typeconversion.UuidStringConverter; import java.util.UUID; @NodeEntity @Getter @Setter public class User { @Id @GeneratedValue(strategy = UuidStrategy.class) @Convert(UuidStringConverter.class) @NonNull private UUID id; }
咱們知道Neo4j的內部ID是會重用的, 所以沒法使用其做爲業務對象的惟一標識. 所以咱們在這裏給對象自動添加"uuid"屬性做爲惟一標識.node
須要 GraphAware Neo4j Framework 和 GraphAware Neo4j UUID 兩個jar包, 下載地址:git
下載頁面
https://graphaware.com/produc...github
框架庫
http://products.graphaware.co...
UUID庫
http://products.graphaware.co...shell
保證graphaware-server-community-all-3.3.2.51.jar
和graphaware-uuid-3.3.2.51.14.jar
兩個庫存在於 Neo4j 安裝目錄的plugins
子目錄之下. 並重啓 Neo4j 服務器.
在conf/neo4j.conf
配置文件中添加以下設置:服務器
com.graphaware.runtime.enabled=true #UIDM becomes the module ID: com.graphaware.module.UIDM.1=com.graphaware.module.uuid.UuidBootstrapper #optional, default is uuid: com.graphaware.module.UIDM.uuidProperty=uuid #optional, default is false: com.graphaware.module.UIDM.stripHyphens=false #optional, default is all nodes: #com.graphaware.module.UIDM.node=hasLabel('Label1') || hasLabel('Label2') #optional, default is no relationships: #com.graphaware.module.UIDM.relationship=isType('Type1') #com.graphaware.module.UIDM.relationship=com.graphaware.runtime.policy.all.IncludeAllBusinessRelationships #optional, default is uuidIndex com.graphaware.module.UIDM.uuidIndex=uuidIndex #optional, default is uuidRelIndex com.graphaware.module.UIDM.uuidRelationshipIndex=uuidRelIndex
<dependency> <groupId>com.graphaware.neo4j</groupId> <artifactId>runtime</artifactId> <version>LATEST</version> </dependency> <dependency> <groupId>com.graphaware.neo4j</groupId> <artifactId>uuid</artifactId> <version>3.3.2.51.14</version> </dependency>
而後在程序中以下使用app
dbms.logs.debug.level=DEBUG com.graphaware.runtime.enabled=true com.graphaware.module.UIDM.1=com.graphaware.module.uuid.UuidBootstrapper com.graphaware.module.UIDM.uuidProperty=uuid com.graphaware.module.UIDM.stripHyphens=false com.graphaware.module.UIDM.uuidIndex=uuidIndex com.graphaware.module.UIDM.uuidRelationshipIndex=uuidRelIndex
public void init() throws Exception {} graphDb = new GraphDatabaseFactory() .newEmbeddedDatabaseBuilder(DATABASE_DIRECTORY) .setConfig(GraphDatabaseSettings.pagecache_memory, "512M") .setConfig(GraphDatabaseSettings.string_block_size, "60") .setConfig(GraphDatabaseSettings.array_block_size, "300") .setConfig(GraphDatabaseSettings.store_internal_log_level, "DEBUG") .loadPropertiesFromFile(path.toAbsolutePath().toString()) .setConfig(bolt.enabled, "true") .setConfig(bolt.type, "BOLT") .setConfig(bolt.listen_address, "0.0.0.0:7687") .setConfig(bolt.encryption_level, BoltConnector.EncryptionLevel.OPTIONAL.toString()) .setConfig(ShellSettings.remote_shell_enabled, Settings.TRUE) .newGraphDatabase(); registerFunction(graphDb, ga.uuid.NodeUuidFunctions.class); registerFunction(graphDb, ga.uuid.RelationshipUuidFunctions.class); Util.registerShutdownHook(graphDb); GraphAwareRuntime runtime = GraphAwareRuntimeFactory.createRuntime(database); // database 是一個 GraphDatabaseService 實例 UuidModule module = new UuidModule("UUIDM", UuidConfiguration.defaultConfiguration()); runtime.registerModule(module); runtime.start(); } /** * 註冊用戶定義函數 * * @param db * @param procedures * @throws KernelException */ public static void registerFunction(GraphDatabaseService db, Class<?>... procedures) throws KernelException { Procedures proceduresService = ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(Procedures.class); for (Class<?> procedure : procedures) { log.info("=== register function: {}", procedure.toString()); proceduresService.registerFunction(procedure); } }