简体   繁体   中英

neo4j template slow persistence when dealing with bidirectional relationships

Given the following two objects:

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

and

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

The following persistence level code is dreadfully slow (5-20 seconds per persist slow)

@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);  
}

Are you using Neo4j REST interface? If so, my this post may provide a clue - Performance with @MapResult in spring-data-neo4j

How many foo's are you adding to bar ?

Are you using simple mapping or advanced mapping?

You should have a @Transactional around your go() method, otherwise there will be thousands of small transactions created for each entity added. (in advanced mode, in simple mode that code doesn't save the changes.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM