Spring 如何在关系实体中保存关系?
我有两个班,第一班是公司:Spring 如何在关系实体中保存关系?,spring,neo4j,spring-data-neo4j,spring-data-neo4j-4,neo4j-ogm,Spring,Neo4j,Spring Data Neo4j,Spring Data Neo4j 4,Neo4j Ogm,我有两个班,第一班是公司: @GraphId private Long graphId; private String id; private String name; 第二类是产品: @GraphId private Long graphId; private String id; private String name; 在这两个类之间,关系是company拥有许可证的,我想创建关系实体,以便在关系中保存属性。 因此,我创建了名为CompanyHasLicenseFor的关系实体
@GraphId
private Long graphId;
private String id;
private String name;
第二类是产品:
@GraphId
private Long graphId;
private String id;
private String name;
在这两个类之间,关系是company拥有许可证的,我想创建关系实体,以便在关系中保存属性。
因此,我创建了名为CompanyHasLicenseFor的关系实体类:
@RelationshipEntity(type="company_has_license_for")
public class CompanyHasLicenseFor {
@GraphId
private Long graphId;
private String id;
@StartNode
private Company company;
@EndNode
private Product product;
private Date startDate;
private Date endDate;
}
这是我用来创建关系的代码:
Company company = new Company("Company Test");
companyService.save(company);
Product product = new Product("Product Test");
productService.save(product);
CompanyHasLicenseFor com = new CompanyHasLicenseFor(company, product, new Date(), new Date());
companyHasLicenseForService.save(com);
当我尝试使用上面的代码创建虚拟数据时,两个节点之间的关系没有创建。仅创建节点公司和产品。
在这种情况下,如何创建/保持关系实体
多谢各位
更新#1:
@NodeEntity
public class Company {
@GraphId
Long graphId;
String id;
String name;
@Relationship(type="company_has_license_for", direction = "UNDIRECTED")
Set<CompanyHasLicenseFor> companylicenses;
.....
}
@NodeEntity
public class Product {
@GraphId
Long graphId;
String id;
String name;
@Relationship(type = "company_has_license_for", direction = "UNDIRECTED")
Set<CompanyHasLicenseFor> companylicenses;
....
}
@RelationshipEntity(type="company_has_license_for")
public class CompanyHasLicenseFor {
@GraphId
Long graphId;
String id;
@StartNode
Company company;
@EndNode
Product product;
....
}
我已尝试将CompanyHasLicenseFor添加为我公司类中的关系属性:
@格拉希德
私人长格拉皮德
private String id;
private String name;
@Relationship(type="company_has_license_for")
private CompanyHasLicenseFor companylicense;
但这种关系仍未建立
更新#2:
@NodeEntity
public class Company {
@GraphId
Long graphId;
String id;
String name;
@Relationship(type="company_has_license_for", direction = "UNDIRECTED")
Set<CompanyHasLicenseFor> companylicenses;
.....
}
@NodeEntity
public class Product {
@GraphId
Long graphId;
String id;
String name;
@Relationship(type = "company_has_license_for", direction = "UNDIRECTED")
Set<CompanyHasLicenseFor> companylicenses;
....
}
@RelationshipEntity(type="company_has_license_for")
public class CompanyHasLicenseFor {
@GraphId
Long graphId;
String id;
@StartNode
Company company;
@EndNode
Product product;
....
}
@NodeEntity
公营公司{
@格拉希德
长葡萄;
字符串id;
字符串名;
@关系(type=“公司”拥有”,direction=“无方向的”的许可证)
设立公司执照;
.....
}
@节点性
公共类产品{
@格拉希德
长葡萄;
字符串id;
字符串名;
@关系(type=“公司”拥有”,direction=“无方向的”的许可证)
设立公司执照;
....
}
@RelationshipEntity(type=“公司拥有许可证”)
公共类公司拥有许可证{
@格拉希德
长葡萄;
字符串id;
@StartNode
公司;
@端节点
产品;
....
}
我使用的是Spring数据Neo4j 4.2.0.RELEASE和Neo4j-ogm-2.1.1
如果需要,这是我的
来自SDN文档:
图中的路径首先从节点开始写入,然后从节点开始写入
它们之间建立了关系。因此,你需要
构造域模型,以便关系实体
可从节点实体访问以使其正常工作
因此,我想您需要将公司作为
实体的一个属性
查看示例代码后更新:
和
您应该确保OGM扫描您的实体和关系。在您的配置(会话工厂初始化)中,您告诉OGM扫描包com.example.neo.model
,但您的关系实体位于包com.example.neo.relationshipModel
,因此它不会被拾取
=>将CompanyHasLicenseFor类移动到扫描包中,或将另一个包添加到会话工厂构造函数中。来自SDN文档:
图中的路径首先从节点开始写入,然后从节点开始写入
它们之间建立了关系。因此,你需要
构造域模型,以便关系实体
可从节点实体访问以使其正常工作
因此,我想您需要将公司作为
实体的一个属性
查看示例代码后更新:
和
您应该确保OGM扫描您的实体和关系。在您的配置(会话工厂初始化)中,您告诉OGM扫描包com.example.neo.model
,但您的关系实体位于包com.example.neo.relationshipModel
,因此它不会被拾取
=>将CompanyHasLicenseFor类移动到扫描包中,或将另一个包添加到会话工厂构造函数中。答案1:
@NodeEntity
public class Company {
@GraphId
Long graphId;
String id;
String name;
@Relationship(type="company_has_license_for", direction = "UNDIRECTED")
Set<CompanyHasLicenseFor> companylicenses;
.....
}
@NodeEntity
public class Product {
@GraphId
Long graphId;
String id;
String name;
@Relationship(type = "company_has_license_for", direction = "UNDIRECTED")
Set<CompanyHasLicenseFor> companylicenses;
....
}
@RelationshipEntity(type="company_has_license_for")
public class CompanyHasLicenseFor {
@GraphId
Long graphId;
String id;
@StartNode
Company company;
@EndNode
Product product;
....
}
我看了一下你的示例项目。问题是,您没有定义关系模型的neo4jconfig包。您需要在会话工厂中定义com.example.neo.relationshipModel的包,如下所示:
@Bean
public SessionFactory sessionFactory() {
// with domain entity base package(s)
return new SessionFactory("com.example.neo.model","com.example.neo.relationshipModel", "BOOT-INF.classes.com.example.neo.model");
}
答案2:
@NodeEntity
public class Company {
@GraphId
Long graphId;
String id;
String name;
@Relationship(type="company_has_license_for", direction = "UNDIRECTED")
Set<CompanyHasLicenseFor> companylicenses;
.....
}
@NodeEntity
public class Product {
@GraphId
Long graphId;
String id;
String name;
@Relationship(type = "company_has_license_for", direction = "UNDIRECTED")
Set<CompanyHasLicenseFor> companylicenses;
....
}
@RelationshipEntity(type="company_has_license_for")
public class CompanyHasLicenseFor {
@GraphId
Long graphId;
String id;
@StartNode
Company company;
@EndNode
Product product;
....
}
如果不想更改neo4jconfig,可以更改模型结构,将公司haslicensefor.java移动到packagecom.example.neo.model
,如下所示:
答案1:
@NodeEntity
public class Company {
@GraphId
Long graphId;
String id;
String name;
@Relationship(type="company_has_license_for", direction = "UNDIRECTED")
Set<CompanyHasLicenseFor> companylicenses;
.....
}
@NodeEntity
public class Product {
@GraphId
Long graphId;
String id;
String name;
@Relationship(type = "company_has_license_for", direction = "UNDIRECTED")
Set<CompanyHasLicenseFor> companylicenses;
....
}
@RelationshipEntity(type="company_has_license_for")
public class CompanyHasLicenseFor {
@GraphId
Long graphId;
String id;
@StartNode
Company company;
@EndNode
Product product;
....
}
我看了一下你的示例项目。问题是,您没有定义关系模型的neo4jconfig包。您需要在会话工厂中定义com.example.neo.relationshipModel的包,如下所示:
@Bean
public SessionFactory sessionFactory() {
// with domain entity base package(s)
return new SessionFactory("com.example.neo.model","com.example.neo.relationshipModel", "BOOT-INF.classes.com.example.neo.model");
}
答案2:
@NodeEntity
public class Company {
@GraphId
Long graphId;
String id;
String name;
@Relationship(type="company_has_license_for", direction = "UNDIRECTED")
Set<CompanyHasLicenseFor> companylicenses;
.....
}
@NodeEntity
public class Product {
@GraphId
Long graphId;
String id;
String name;
@Relationship(type = "company_has_license_for", direction = "UNDIRECTED")
Set<CompanyHasLicenseFor> companylicenses;
....
}
@RelationshipEntity(type="company_has_license_for")
public class CompanyHasLicenseFor {
@GraphId
Long graphId;
String id;
@StartNode
Company company;
@EndNode
Product product;
....
}
如果不想更改neo4jconfig,可以更改模型结构,将公司haslicensefor.java移动到packagecom.example.neo.model
,如下所示:
是否公司服务
、产品服务
和公司拥有服务许可证
是否都是Spring数据存储库
s?如果是这样的话,这一切都应该起作用。如果您想让三个保存方法成为原子操作,您可能需要一个@Transactional
围绕在这三个保存方法周围。@digx1 Yes所有服务都将调用存储库函数。我已经将事务注释放在公共类声明之上。但是在neo4jArecompanyService
、productService
和companyhasslicenseforservice
中仍然没有创建关系,这些都是Spring数据存储库
s吗?如果是这样的话,这一切都应该起作用。如果您想让三个保存方法成为原子操作,您可能需要一个@Transactional
围绕在这三个保存方法周围。@digx1 Yes所有服务都将调用存储库函数。我已经将事务注释放在公共类声明之上。但是neo4jhi中仍然没有建立这种关系,我已经尝试过你的解决方案,并在更新1中的问题中添加了代码,这就是你的意思吗?因为即使在我将关系属性添加到我的类中之后,关系仍然没有创建。在保存之前,对象图是否一致?i、 e.你设置了company.companyLicense吗?@luane不,我在保存关系实体后没有设置company.companyLicense。我需要这样做吗?@Luane我只是在更新2更新了我的问题。但结果还是一样,不是吗