[Neo4j] Copy all the Relationships to another Node

528 views
Skip to first unread message

Emil Dombagolla

unread,
Jan 12, 2012, 1:27:25 AM1/12/12
to us...@lists.neo4j.org
Hi All,

I am using Java API with neo4j 1.4.2.

I want to copy all the relationships(Income/Outgoing) of a node to another
node and delete the original node. Can anyone suggest what will be the
easiest way.

Emil Dombagolla,
Thank you so much.
_______________________________________________
NOTICE: THIS MAILING LIST IS BEING SWITCHED TO GOOGLE GROUPS, please register and consider posting at https://groups.google.com/forum/#!forum/neo4j

Neo4j mailing list
Us...@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user

Mattias Persson

unread,
Jan 12, 2012, 4:42:37 AM1/12/12
to Neo4j user discussions
A straightforward:

Node target = db.createNode();
copyProperties( source, target );
for ( Relationship sourceRel : source.getRelationships() ) {
Relationship targetRel = source.equals( sourceRel.getStartNode() ) ?
target.createRelationshipTo( sourceRel.getEndNode(),
sourceRel.getType() ) :
sourceRel.getEndNode().createRelationshipTo( target,
sourceRel.getType() );
copyProperties( sourceRel, targetRel );
sourceRel.delete();
}
source.delete();
...
private static void copyProperties( PropertyContainer source,
PropertyContainer target ) {
for ( String key : source.getPropertyKeys() )
target.setProperty( key, source.getProperty( key ) );
}

would suffice, I'd guess.

2012/1/12 Emil Dombagolla <em...@hsenidoutsourcing.com>

--
Mattias Persson, [mat...@neotechnology.com]
Hacker, Neo Technology
www.neotechnology.com

Emil Dombagolla

unread,
Jan 12, 2012, 6:01:21 AM1/12/12
to ne...@googlegroups.com
Thanks you so much. 
--
Emil Dombagolla,
Software Engineer
hSenid Software International

Phone : +94-11-2699754  
Fax     : +94-11-2673845
mobile : +94-71-9433348

Web    : http://www.hSenid.com
"Make it Happen"

Web    : http://www.hSenidOutsourcing.com
"Making you Competitive"

Disclaimer: This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to which they are addressed. The content and opinions contained in this email are not necessarily those of hSenid Software International. If you have received this email in error please contact the sender.




Reply all
Reply to author
Forward
0 new messages