无法在Spring Data 中保存Relationship属性值Neo4j 3.1

dm7nw8vv  于 2023-06-29  发布在  Spring
关注(0)|答案(1)|浏览(210)

我有一个简单的用例,我有n个商店-->,其中有k个产品。我想将商店与关系HasProduct连接起来。在这种关系,我想捕捉产品的数量,价格,因为每个商店的数量和价格将是不同的。

@Data
@Node
public class Store {

    @Id
    @GeneratedValue
    private Long id;
    @Property
    private String businessType;
    @Property
    private int currentOnhandInventory;
    @Property
    private String locationId;
    @Property
    @Relationship(type = "HasProduct", value = "HasProduct", direction = Direction.OUTGOING)
    private List<Product> topSellingProducts;
}

@Data
@Node
public class Product {

    @Id
    @GeneratedValue
    private Long id;
    @Property
    private String name;
    @Property
    private String description;
    @Property
    private String department;
    @Property
    @Relationship
    private Store store;

}

@Data
@RelationshipProperties
public class HasProduct {

    @RelationshipId
    @GeneratedValue
    private Long id;

    @Property
    private String qty;

    @Property
    private Double price;

    @TargetNode
    private Product product;

}

@RestController
@RequestMapping("/store")
public class StoreController {

    private StoreRepository storeRepository;
    
    public StoreController(StoreRepository storeRepository) {
        this.storeRepository = storeRepository;
    }

    @PutMapping
    Mono<Store> createOrUpdateStore(@RequestBody Store newStore) {
        return storeRepository.save(newStore);
    }

    @GetMapping
    Mono<Store> getStoreById(@RequestParam Long id) {
        return storeRepository.findById(id);
    }
}

@org.springframework.context.annotation.Configuration
public class Config {

    
    @Bean(ReactiveNeo4jRepositoryConfigurationExtension.DEFAULT_TRANSACTION_MANAGER_BEAN_NAME)
    public ReactiveTransactionManager reactiveTransactionManager(
        Driver driver,
        ReactiveDatabaseSelectionProvider databaseNameProvider) {
        return new ReactiveNeo4jTransactionManager(driver, databaseNameProvider);
    }
    
    @Bean
    public Configuration cypherDslConfiguration() {
        return Configuration.newConfig()
            .withDialect(Dialect.NEO4J_5)
            .build();
    } 
}

public interface StoreRepository extends ReactiveNeo4jRepository<Store, Long> {
    Mono<List<Store>> findOneById(Long id);
}

当我试图保存一个存储对象时,我得到这个错误

java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "valueToCheck" is null
    at org.springframework.data.neo4j.core.mapping.NestedRelationshipProcessingStateMachine.hasProcessedValue(NestedRelationshipProcessingStateMachine.java:178) ~[spring-data-neo4j-7.1.0.jar:7.1.0]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Assembly trace from producer [reactor.core.publisher.FluxPeek] :
    reactor.core.publisher.Flux.checkpoint(Flux.java:3454)
    org.springframework.data.neo4j.core.ReactiveNeo4jTemplate.processNestedRelations(ReactiveNeo4jTemplate.java:1049)

有人能帮忙吗,因为Spring data 3.1没有太多的文档或示例。

5m1hhzi4

5m1hhzi41#

您应该需要通过将关系属性类放到域类中来Map关系属性类。我喜欢将关系属性类看作一个单独的实体,您需要将节点链接到节点的关系。所以,在Store类中,你需要这样的东西:

@Relationship("HasProduct")
private List<HasProduct> topSellingProducts;

举几个小例子:

相关问题