SDN - unique relationships and relateTo?

29 views
Skip to first unread message

Tero Paananen

unread,
May 15, 2012, 2:15:05 PM5/15/12
to Neo4j
Michael,

I finally have some time to look into the unique relationship changes
in SDN 2.1.0 RC1.

Currently I'm doing this (illustrative):

Map<String, Object> properties = getProperties();
MyRel rel = null;
Index<PropertyContainer> relIndex = template.getIndex("relexact");
IndexHits<PropertyContainer> indexHits = relIndex.get("uniqueKey",
"uniqueValue");
if (!indexHits.hasNext()) {
rel = startNode.relateTo(endNode, MyRel.class, "REL", true);
} else {
PropertyContainer container = indexHits.next();
rel = template.projectTo(container, MyRel.class);
}
rel.copyProperties(properties);
return rel;


Looking at the code changes, I should no longer use relateTo, but
the template.getOrCreateRelationship() method:

Relationship relEntity = template.getOrCreateRelationship(
"relexact",
"uniqueKey",
"uniqueValue",
user.getPersistentState(),
employer.getPersistentState(),
"REL",
properties);

MyRel rel = myRelRepository.findOne(relEntity.getId());

Is this the right way to do it?

Is there an easier way to get from the Relationship object
to the SDN RelationshipEntity object (in my example MyRel
class)?

-TPP

Michael Hunger

unread,
May 15, 2012, 4:30:20 PM5/15/12
to ne...@googlegroups.com
Looks good.

It only has an caveat, as in neo4j the uniqueness only looks at the index-key-value combination, neither on rel-type, start or end-node.

You can use template.createEnttiyFromStoredState() to get your entity from the relationship.

You can also use @Indexed(unique=true) inside the rel-entity and just do template.save(myRel)

Cheers

Michael

Tero Paananen

unread,
May 15, 2012, 4:55:32 PM5/15/12
to ne...@googlegroups.com
> It only has an caveat, as in neo4j the uniqueness only looks at the index-key-value
> combination, neither on rel-type, start or end-node.

Yep.

> You can use template.createEnttiyFromStoredState() to get your entity from the relationship.
>
> You can also use @Indexed(unique=true) inside the rel-entity and just do template.save(myRel)

In this case, how do I deal with the start and end nodes?

MyRel rel = new MyRel();
// set other properties
rel.setStartNode(startNode);
rel.setEndNode(endNode);

Can I do that?

-TPP

Michael Hunger

unread,
May 15, 2012, 5:02:33 PM5/15/12
to ne...@googlegroups.com
Yep,

template.save(new MyRel(start,end,type,otherProps));

Michael

Am 15.05.2012 um 22:55 schrieb Tero Paananen:

>> It only has an caveat, as in neo4j the uniqueness only looks at the index-key-value
>> combination, neither on rel-type, start or end-node.
>
> Yep.
>
>> You can use template.createEnttiyFromStoredState() to get your entity from the relationship.
>>
>> You can also use @Indexed(unique=true) inside the rel-entity and just do template.save(myRel)
>
> In this case, how do I deal with the start and end nodes?
>
you have annotated fields in your relationship-entity:
@StartNode StartEntity start;
@EndNode EndEntity end;

you can/should also set type on @RelationshipEnttiy(type=...)
or
@RelType String type;

Michael

Tero Paananen

unread,
May 15, 2012, 5:10:36 PM5/15/12
to ne...@googlegroups.com
On Tue, May 15, 2012 at 5:02 PM, Michael Hunger
<michael...@neotechnology.com> wrote:
> Yep,
>
> template.save(new MyRel(start,end,type,otherProps));
>
> you can/should also set type on @RelationshipEnttiy(type=...)
> or
> @RelType String type;

Ah, gotcha.

I've been using relateTo() everywhere so far, which has a different
usage pattern...create an "empty" relationship entity, set properties
on it afterwards.

Thanks!

-TPP

Michael Hunger

unread,
May 15, 2012, 5:48:25 PM5/15/12
to ne...@googlegroups.com
Yep, relateTo (or createRelationshipBetween()) is sensible, much like storing the relationship entity directly.

Both are faster than handling relationship-collections.

Michael
Reply all
Reply to author
Forward
0 new messages