如何在关系实体中保存关系?
How to save relationship in relationship entity?
我有两个 class 第一个是公司 :
@GraphId
private Long graphId;
private String id;
private String name;
第二个 class 是产品:
@GraphId
private Long graphId;
private String id;
private String name;
在这两个 class 之间的关系是公司有许可的,我想创建关系实体,这样我就可以在关系中保存 属性。
所以我创建了这个名为 CompanyHasLicenseFor 的关系实体 class :
@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);
当我尝试使用上面的代码创建虚拟数据时,两个节点之间的关系没有创建。仅创建节点公司和产品。
在这种情况下如何 create/persist 关系实体?
谢谢
更新#1:
我已经尝试在我的公司 class 中添加 CompanyHasLicenseFor 作为关系 属性:
@GraphId
private Long graphId;
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;
....
}
我正在使用 Spring Data Neo4j 4.2.0.RELEASE 和 neo4j-ogm-2.1.1
如果需要,这是我的 Sample Code
Screenshot of my database
来自 SDN 文档:
paths in the graph are written starting with nodes first and then
relationships are created between them. Therefore, you need to
structure your domain models so that relationship entities are
reachable from node entities for this to work correctly
所以我猜你需要让 CompanyHasLicenseFor
成为你实体的一个属性。
看完示例代码更新:
AND
您应该确保 OGM 扫描了您的实体和关系。在你的配置(会话工厂初始化)中,你告诉 OGM 扫描包 com.example.neo.model
但你的关系实体存在于包 com.example.neo.relationshipModel
中,所以它没有被拾取。
=> 在扫描包中移动 CompanyHasLicenseFor class 或在会话工厂构造函数中添加其他包。
回答 1 :
我看看你的示例项目。问题是,您没有定义关系模型的 neo4jconfig 包。您需要像这样在 SessionFactory 定义 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:
如果您不想更改您的 neo4jconfig,您可以更改您的模型结构,将 CompanyHasLicenseFor.java 移动到包 com.example.neo.model
中,如下所示:
我有两个 class 第一个是公司 :
@GraphId
private Long graphId;
private String id;
private String name;
第二个 class 是产品:
@GraphId
private Long graphId;
private String id;
private String name;
在这两个 class 之间的关系是公司有许可的,我想创建关系实体,这样我就可以在关系中保存 属性。 所以我创建了这个名为 CompanyHasLicenseFor 的关系实体 class :
@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);
当我尝试使用上面的代码创建虚拟数据时,两个节点之间的关系没有创建。仅创建节点公司和产品。 在这种情况下如何 create/persist 关系实体?
谢谢
更新#1:
我已经尝试在我的公司 class 中添加 CompanyHasLicenseFor 作为关系 属性: @GraphId private Long graphId;
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;
....
}
我正在使用 Spring Data Neo4j 4.2.0.RELEASE 和 neo4j-ogm-2.1.1
如果需要,这是我的 Sample Code
Screenshot of my database
来自 SDN 文档:
paths in the graph are written starting with nodes first and then relationships are created between them. Therefore, you need to structure your domain models so that relationship entities are reachable from node entities for this to work correctly
所以我猜你需要让 CompanyHasLicenseFor
成为你实体的一个属性。
看完示例代码更新:
AND
您应该确保 OGM 扫描了您的实体和关系。在你的配置(会话工厂初始化)中,你告诉 OGM 扫描包 com.example.neo.model
但你的关系实体存在于包 com.example.neo.relationshipModel
中,所以它没有被拾取。
=> 在扫描包中移动 CompanyHasLicenseFor class 或在会话工厂构造函数中添加其他包。
回答 1 :
我看看你的示例项目。问题是,您没有定义关系模型的 neo4jconfig 包。您需要像这样在 SessionFactory 定义 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:
如果您不想更改您的 neo4jconfig,您可以更改您的模型结构,将 CompanyHasLicenseFor.java 移动到包 com.example.neo.model
中,如下所示: