Database Reference
In-Depth Information
The preceding code will fetch the value of the attributes defined directly in
Role.java
,
but it will not fetch the values of the referenced objects such as
Artist
and
Movie
.
In order to fetch the values of the referenced objects, we need to define the
@Fetch
an-
notation for the referenced object, so that Spring can eagerly load the values of the refer-
enced objects. By default, relationships do not fetch the values unless we ask them to do
so. This is also beneficial in scenarios where we have large graphs and do not want to load
everything in memory.
For example,
Role.java
will be modified to eagerly load the object's data—
Artist
and
Movie
—as shown in the following code:
package org.neo4j.spring.samples.domain;
import org.springframework.data.neo4j.annotation.*;
@RelationshipEntity(type="ACTED_IN")
public class Role {
@StartNode @Fetch
private Artist artist;
@EndNode @Fetch
private Movie movie;
private String roleName;
…
//Define getters and setters for all properties.
}
There are two more important annotations which can be used with our domain model / en-
tities:
•
@RelatedTo
: This annotation is used for the fields to define the type of rela-
tionship within the node entity. For example, we can define a collection in
Artist.java
and define the relationship with the
Movie
object there itself:
@RelatedTo(type="ACTED_IN",
direction=Direction.OUTGOING)
private Set<Movie> movies;