neo4j模板在处理双向关系时缓慢持久化

时间:2013-04-10 12:27:41

标签: java performance neo4j

给出以下两个对象:

@NodeEntity  
class Foo  
{
   @GraphId   
   long id;  
   @RelatedTo(Bar.class,direction=Direction.BOTH, type="BAR")  
   Set<Bar> bars= new HashSet<Bar>();    
   @Indexed  
   String name;
}  

@NodeEntity  
class Bar
{
   @GraphId   
   long id;  
   @RelatedTo(Foo.class,direction=Direction.BOTH, type="Foo")  
   Set<Foo> foos= new HashSet<Foo>();    
   @Indexed  
   String name;
}    

以下持久性级别代码非常慢(每个持续缓慢5-20秒)

@Service  
class Persister  
{  
   @Autowired  
   Neo4JTemplate template;    
   @Autowired  
   FooRepository fooRepo;    
   @Autowired  
   BarRepository barRepo;  
void go()  
{  
    Bar bar = barRepo.findByName("myBar");  
    if(null != bar)  
    {    
        bar.getFoos().addAll(fooRepo.readAll());    
        return bar;  
    }
     template.save(bar);  
 }    
}

interface BarRepository extends Repository<Bar>  
{  
     Bar findByName(String name);  
}

2 个答案:

答案 0 :(得分:0)

您使用的是Neo4j REST界面吗?如果是这样,我的这篇文章可能会提供线索 - Performance with @MapResult in spring-data-neo4j

答案 1 :(得分:0)

你在bar添加了多少foo?

您使用的是简单映射还是高级映射?

您的@Transactional方法应该有一个go(),否则会为添加的每个实体创建数千个小交易。 (在高级模式下,在简单模式下,代码不保存更改。)