Made relationship type attribute of RelatedTo optional, defaulting to the field name (modulo useShortNames).

This commit is contained in:
David Montag
2011-03-21 15:59:45 -07:00
parent 0fcadd026b
commit d0ff6c8be2
8 changed files with 17 additions and 13 deletions

View File

@@ -16,14 +16,14 @@
package org.springframework.data.graph.annotation;
import org.springframework.data.graph.core.Direction;
import org.springframework.data.graph.core.NodeBacked;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.data.graph.core.Direction;
import org.springframework.data.graph.core.NodeBacked;
/**
* Annotation for {@link org.springframework.data.graph.annotation.NodeEntity} fields that relate to other entities via
* relationships. Works for one-to-one and one-to-many relationships. It is optionally possible to define the relationship type,
@@ -31,10 +31,11 @@ import org.springframework.data.graph.core.NodeBacked;
*
* Collection based one-to-many relationships return managed collections that reflect addition and removal to the underlying relationships.
*
* Examples:
* <pre>
* &#64;RelatedTo([type=&quot;friends&quot;], elementClass=Person.class)
* &#64;RelatedTo(elementClass=Person.class)
* Collection&lt;Person&gt; friends;
* &#64;RelatedTo([type=&quot;spouse&quot;], [elementClass=Person.class])
* &#64;RelatedTo(type=&quot;partner&quot;)
* Person spouse;
* </pre>
@@ -47,7 +48,7 @@ public @interface RelatedTo {
/**
* @return name of the relationship type, optional, can be inferred from the field name
*/
String type();
String type() default "";
/**
* @return direction for the relationship, by default outgoing

View File

@@ -58,6 +58,10 @@ abstract class NodeRelationshipFieldAccessorFactory implements FieldAccessorFact
return DynamicRelationshipType.withName(relAnnotation.type());
}
protected DynamicRelationshipType typeFrom(Field field, RelatedTo relAnnotation) {
return "".equals(relAnnotation.type()) ? typeFrom(field) : typeFrom(relAnnotation);
}
protected RelatedTo getRelationshipAnnotation(Field field) {
return field.getAnnotation(RelatedTo.class);
}

View File

@@ -44,7 +44,7 @@ public class OneToNRelationshipFieldAccessorFactory extends NodeRelationshipFiel
@Override
public FieldAccessor<NodeBacked> forField(final Field field) {
final RelatedTo relAnnotation = getRelationshipAnnotation(field);
return new OneToNRelationshipFieldAccessor(typeFrom(relAnnotation), dirFrom(relAnnotation), targetFrom(relAnnotation), graphDatabaseContext);
return new OneToNRelationshipFieldAccessor(typeFrom(field, relAnnotation), dirFrom(relAnnotation), targetFrom(relAnnotation), graphDatabaseContext);
}
public static class OneToNRelationshipFieldAccessor extends NodeToNodesRelationshipFieldAccessor<NodeBacked> {

View File

@@ -39,7 +39,7 @@ public class ReadOnlyOneToNRelationshipFieldAccessorFactory extends NodeRelation
@Override
public FieldAccessor<NodeBacked> forField(final Field field) {
final RelatedTo relAnnotation = getRelationshipAnnotation(field);
return new ReadOnlyOneToNRelationshipFieldAccessor(typeFrom(relAnnotation), dirFrom(relAnnotation), targetFrom(relAnnotation), graphDatabaseContext);
return new ReadOnlyOneToNRelationshipFieldAccessor(typeFrom(field, relAnnotation), dirFrom(relAnnotation), targetFrom(relAnnotation), graphDatabaseContext);
}
public static class ReadOnlyOneToNRelationshipFieldAccessor extends OneToNRelationshipFieldAccessorFactory.OneToNRelationshipFieldAccessor {

View File

@@ -24,7 +24,6 @@ import org.springframework.data.graph.core.NodeBacked;
import org.springframework.data.graph.neo4j.support.GraphDatabaseContext;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
@@ -46,7 +45,7 @@ public class SingleRelationshipFieldAccessorFactory extends NodeRelationshipFiel
final RelatedTo relAnnotation = getRelationshipAnnotation(field);
if (relAnnotation == null)
return new SingleRelationshipFieldAccessor(typeFrom(field), Direction.OUTGOING, targetFrom(field), graphDatabaseContext);
return new SingleRelationshipFieldAccessor(typeFrom(relAnnotation), dirFrom(relAnnotation), targetFrom(field), graphDatabaseContext);
return new SingleRelationshipFieldAccessor(typeFrom(field, relAnnotation), dirFrom(relAnnotation), targetFrom(field), graphDatabaseContext);
}
public static class SingleRelationshipFieldAccessor extends NodeToNodesRelationshipFieldAccessor<NodeBacked> {

View File

@@ -26,7 +26,7 @@ public class Group {
public final static String OTHER_NAME_INDEX="other_name";
public static final String SEARCH_GROUPS_INDEX = "search-groups";
@RelatedTo(type = "persons", direction = Direction.OUTGOING, elementClass = Person.class)
@RelatedTo(direction = Direction.OUTGOING, elementClass = Person.class)
private Collection<Person> persons;
@RelatedTo(type = "persons", elementClass = Person.class)

View File

@@ -44,7 +44,7 @@ public class Person {
private Car car;
@RelatedTo(type = "mother", direction = Direction.OUTGOING)
@RelatedTo
private Person mother;
@RelatedTo(type = "boss", direction = Direction.INCOMING)

View File

@@ -61,7 +61,7 @@ public class NodeEntityRelationshipTest {
Person p = persistedPerson("Michael", 35);
Person mother = persistedPerson("Gabi", 60);
p.setMother(mother);
Node motherNode = p.getPersistentState().getSingleRelationship(DynamicRelationshipType.withName("mother"), Direction.OUTGOING).getEndNode();
Node motherNode = p.getPersistentState().getSingleRelationship(DynamicRelationshipType.withName("Person.mother"), Direction.OUTGOING).getEndNode();
assertEquals(mother.getPersistentState(), motherNode);
assertEquals(mother, p.getMother());
}