Neo4j自定义主键策略
package com.huajun.kgbuilder.util; import org.neo4j.ogm.id.IdStrategy; /** * 于吉利 * 2021-08-26 * Neo4j自定义主键策略 */ public class Neo4JCustomIdStrategy implements IdStrategy { @Override public Object generateId(Object o) { return o.hashCode(); } }
@Id
@GeneratedValue(strategy = Neo4JCustomIdStrategy.class)
private String id;
以上只适用非cloud项目,引用的文件是
import org.neo4j.ogm.id.IdStrategy;
spring cloud Alibaba 微服务项目引用
import org.springframework.data.neo4j.core.schema.IdGenerator
/* * @Copyright (c) 2021. 于吉利 All Rights Reserved * @Author: 于吉利 * @Function: * @Date: 2021/8/27 上午11:50 * @version:1.0 * @Changes: * @Description: * */ package com.huajun.kgbuilder.util; import java.util.concurrent.atomic.AtomicInteger; import org.springframework.data.neo4j.core.schema.IdGenerator; import org.springframework.util.StringUtils; public class SequenceGenerator implements IdGenerator<String> { private final AtomicInteger sequence = new AtomicInteger(0); @Override public String generateId(String primaryLabel, Object entity) { return StringUtils.uncapitalize(primaryLabel) + "-" + sequence.incrementAndGet(); } }