使用spring-data-neo4j,我想创建两个类,@RelationshipEntity(type="OWNS")用于将Person类链接到a Pet和Car.
@RelationshipEntity(type="OWNS")
public class OwnsCar {
@Indexed
private String name;
@StartNode
private Person person;
@EndNode
private Car car;
}
@RelationshipEntity(type="OWNS")
public class OwnsPet {
@Indexed
private String name;
@EndNode
private Person person;
@StartNode
private Pet pet;
}
Run Code Online (Sandbox Code Playgroud)
这保存到图形数据库正常,没有任何问题,因为我可以查询的实际Node和Relationship,看看他们键入等.
但是当我尝试使用时,@RelatedTo(type="OWNS", elementClass=Pet.class)我要么获得类强制转换异常,要么在使用延迟初始化时,我得到的结果不正确.
@NodeEntity
public class Person {
@Indexed
private String name;
@RelatedTo(type="OWNS", direction=Direction.OUTGOING, elementClass=Pet.class)
private Set<Pet> pets;
@RelatedTo(type="OWNS", direction=Direction.OUTGOING, elementClass=Car.class)
private Set<Car> cars;
}
Run Code Online (Sandbox Code Playgroud)
我尝试打印我的人时得到的结果(我toString() …
我是Neo4J的新手,我有一个简单的问题.
我的应用程序中有NodeEntity,一个属性(名称)用@Indexed(unique = true)注释,以实现像我在JPA中使用@Column(unique = true)那样的唯一性.
我的问题是,当我持有一个名称已经存在于我的图表中的实体时,无论如何它都能正常工作.但是我在这里期待某种例外......?!以下是我的基本代码的概述:
@NodeEntity
public abstract class BaseEntity implements Identifiable
{
@GraphId
private Long entityId;
...
}
public class Role extends BaseEntity
{
@Indexed(unique = true)
private String name;
...
}
public interface RoleRepository extends GraphRepository<Role>
{
Role findByName(String name);
}
@Service
public class RoleServiceImpl extends BaseEntityServiceImpl<Role> implements
{
private RoleRepository repository;
@Override
@Transactional
public T save(final T entity) {
return getRepository().save(entity);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的考验:
@Test
public void testNameUniqueIndex() {
final List<Role> roles = Lists.newLinkedList(service.findAll());
final …Run Code Online (Sandbox Code Playgroud) 如何通过Spring Roo 1.2.x使用Neo4J?
书籍和在线文档似乎表明Neo4j是Spring Roo 1.2.4的搜索插件.虽然我可以手动添加spring-data-neo4j依赖项,但我真的想利用Roo功能.
Spring Roo 1.2.4.RELEASE [rev 75337cf]
roo> addon search graph
0 found, sorted by rank; T = trusted developer; R = Roo 1.2 compatible
ID T R DESCRIPTION -------------------------------------------------------------
[HINT] use 'addon info id --searchResultId ..' to see details about a search result
[HINT] use 'addon install id --searchResultId ..' to install a specific search result, or
[HINT] use 'addon install bundle --bundleSymbolicName TAB' to install a specific add-on version
Run Code Online (Sandbox Code Playgroud) 我使用Spring 4.1.6.RELEASE和Spring Boot 1.2.3.RELEASE.现在,我无法顺利地从Neo4j 2.1.7和SDN 3.2.2.RELEASE迁移到Neo4j 2.2.0和SDN 3.3.0.RELEASE
首先,Neo4jHelper课程缺席......那么应该使用什么呢?
此外,我的测试崩溃,但有以下例外:
org.springframework.dao.InvalidDataAccessApiUsageException: nested exception is org.neo4j.graphdb.NotInTransactionException
at org.springframework.data.neo4j.support.Neo4jExceptionTranslator.translateExceptionIfPossible(Neo4jExceptionTranslator.java:51)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
Run Code Online (Sandbox Code Playgroud)
但在以前的版本中一切正常(我的DAO和服务都注释了@Transactional).
如何在我的Spring Boot应用程序中为Neo4j配置适当的TX管理器?
现在NullTransactionManager使用,我认为这是问题的原因:
2015-04-03 11:40:36 [main] INFO o.s.c.a.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@12db5286: startup date [Fri Apr 03 11:40:36 EEST 2015]; root of context hierarchy
2015-04-03 11:40:37 [main] INFO o.s.b.f.a.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2015-04-03 11:40:38 [main] INFO o.s.t.jta.JtaTransactionManager - Using JTA UserTransaction: …Run Code Online (Sandbox Code Playgroud) 我在使用时查询深度2时出现超时.我session.load()正在使用Neo4j OGM 1.1.3(尝试从Spring Data Neo4j 3.4迁移).尝试加载Node对象
class Node {
Long id;
String name;
@Relationship(type="NodeToCategory")
Category category;
@Realtionship(type="NodeToChildNode")
Node node
}
class Category {
Long id;
String name;
String color;
Date createdAt;
}
Run Code Online (Sandbox Code Playgroud)
连接到我的节点的类别非常受欢迎(20,000个节点具有相同的类别),当我使用运行时session.load(Node.class, 1L, 2),请求超时.它是否可以尝试查询该类别的所有关系(即使我的Java中的模型类别忽略了这种关系)?
我期望它加载的只是:
|My Node
| |category
| |child node
| | | category
| | | child node
Run Code Online (Sandbox Code Playgroud)
这不是一个非常繁重的请求,不应该超时(除非它加载不需要的关系.
有没有办法告诉只有负载某些关系深入?
例如,我想加载一个树的10个级别,但我的树上的其他信息(如类别,角色不是真正的树节点,但只是代表更多的信息)我只想加载它们没有他们的关系.所以我想加载所有Node对象,只在没有关系的情况下加载其他对象.
UPDATE
发现两个似乎解决这些问题的未解决问题:
我正在尝试使用Spring Data Neo4j,Spring Data Rest和Spring Security 使用Spring Boot(版本1.4.0.M2)创建REST API .域由三种类型的实体组成:
@NodeEntity
public class User extends Entity {
@Relationship(type = "OWNS")
private Set<Activity> activities;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
private String username;
}
Run Code Online (Sandbox Code Playgroud)
@NodeEntity
public class Activity extends Entity {
private String name;
@Relationship(type = "ON", direction = Relationship.INCOMING)
private Set<Period> periods;
@Relationship(type = "HAS", direction = Relationship.INCOMING)
private User user;
}
Run Code Online (Sandbox Code Playgroud)
@NodeEntity
public class Period extends Entity {
@Relationship(type = "HAS")
private Set<Activity> activities; …Run Code Online (Sandbox Code Playgroud) 我想在我的neo4j数据库中存储一些数据.我使用spring-data-neo4j.
我的代码如下:
for (int i = 0; i < newRisks.size(); i++) {
myRepository.save(newRisks.get(i));
System.out.println("saved " + newRisks.get(i).name);
}
Run Code Online (Sandbox Code Playgroud)
我的newRisks-array包含大约60000个对象和60000个边.每个节点和边都有一个属性.这个循环的持续时间大约是15到20分钟,这是正常的吗?我使用Java VisualVM来搜索一些瓶颈,但我的平均CPU使用率为10 - 25%(4个核心),而我的堆不到一半.
有什么选择可以提升这个操作吗?
编辑:额外的是,在第myRepository.save(newRisks.get(i));一次输出即将到来之前的几分钟,jvm 第一次调用下降的fpr
第二次编辑:
类风险:
@NodeEntity
public class Risk {
//...
@Indexed
public String name;
@RelatedTo(type = "CHILD", direction = Direction.OUTGOING)
Set<Risk> risk = new HashSet<Risk>();
public void addChild(Risk child) {
risk.add(child);
}
//...
}
Run Code Online (Sandbox Code Playgroud)
创造风险:
@Autowired
private Repository myRepository;
@Transactional
public Collection<Risk> makeSomeRisks() {
ArrayList<Risk> newRisks = new ArrayList<Risk>();
newRisks.add(new Risk("Root"));
for (int i …Run Code Online (Sandbox Code Playgroud) 这应该是一件简单的事情!但到目前为止我一直无法找到答案.要么我错过了一些明显的东西,否则我会遗漏一些明显的东西......
人说,我有一节课.有三个字段 - "id","name"和"reputation".让我们说我愿意更新"名称"而不是"声誉".我希望Spring Data在从DB中检索时获取"信誉"的值,但在保存bean时忽略它.
@Transient注释就在那里,但是Spring完全忽略了该字段并且根本没有填充它.理想情况下,我正在寻找类似@ReadOnly注释的东西.
更多细节
我在两个playframework 2.3.8服务器上运行Spring Data Neo4j ,迁移到新的spring数据后neo4j(3.3.0)从每个服务器的查询得到不同的结果.这在我使用3.2.1版之前没有发生过.我使用neo4j作为服务器而不是嵌入式.
我的站点显示用户更改的状态(NORMAL或ERROR).通过轮询两个服务器之一,每2秒更新一次状态.问题是我有时间每个服务器返回不同的结果.因此,尽管数据库中的状态是稳定的(由neo4j控制台确认),但每次轮询不同服务器时,用户都会快速更改.
我想也许我从服务器获得缓存结果但是在新的弹簧数据中找不到任何关于缓存的信息可以解释这一点.
我注意到这个问题发生在我使用存储库findOne函数时,当我切换到使用带有查询的新函数时(@query("match n where id(n) = {0} return n"))它停止发生.它发生在其他一些地方,包括我使用的时候template.fetch(o).它也可能发生在我不知道的其他地方.
neo4j playframework spring-data spring-data-neo4j playframework-2.3
我的问题是为什么WHERE运营商的工作速度不如预期的那么快?考虑我有7个带标签的节点Consumer.这是一些示例数据......
MERGE (c:Consumer {mobileNumber: "000000000000"})
MERGE (:Consumer {mobileNumber: "111111111111"})
MERGE (:Consumer {mobileNumber: "222222222222"})
MERGE (:Consumer {mobileNumber: "333333333333"})
MERGE (:Consumer {mobileNumber: "444444444444"})
MERGE (:Consumer {mobileNumber: "555555555555"})
MERGE (:Consumer {mobileNumber: "666666666666"})
WITH c
MATCH (c1:Consumer) WHERE c1.mobileNumber <> "000000000000"
MERGE (c)-[:HAS_CONTACT]->(c1)
Run Code Online (Sandbox Code Playgroud)
并且HAS_CONTACT在:Consumer(mobileNumber:{"000000000000"})所有其他6个节点之间存在关系.unique index对mobileNumber场地也有约束.现在当我尝试执行以下查询时:
PROFILE MATCH (n:Consumer{mobileNumber : "000000000000"}),
(m:Consumer{mobileNumber : "111111111111"})
WITH n,m
MATCH path = SHORTESTPATH((n)-[contacts:HAS_CONTACT]-(m))
RETURN contacts;
Run Code Online (Sandbox Code Playgroud)
所以它的工作正常如预期(基于唯一索引的搜索节点).以下是其结果:

现在让我们使用WHERE子句更改以上查询:
PROFILE MATCH (n:Consumer{mobileNumber : "000000000000"}),
(m:Consumer) WHERE …Run Code Online (Sandbox Code Playgroud) neo4j ×8
java ×2
spring ×2
spring-data ×2
cypher ×1
jvm ×1
neo4j-ogm ×1
spring-boot ×1
spring-roo ×1