Made relationship type attribute of RelatedTo optional, defaulting to the field name (modulo useShortNames).
This commit is contained in:
@@ -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>
|
||||
* @RelatedTo([type="friends"], elementClass=Person.class)
|
||||
* @RelatedTo(elementClass=Person.class)
|
||||
* Collection<Person> friends;
|
||||
* @RelatedTo([type="spouse"], [elementClass=Person.class])
|
||||
* @RelatedTo(type="partner")
|
||||
* 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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user