This lesson covers the basics of projections in Spring Data Neo4j. You will learn what a projection is, when it is useful, as well as how to create and use it in your application.
What is a projection?
Projections contain some subset or manipulation of a domain entity and act like a "view" of that entity. There are a few different situations where projections are invaluable, mostly centered around needing customized versions of a large object.
For example, if you have a domain entity with many properties or large values, it becomes cumbersome to work with, alter, and transport. You can create a projection that only contains a few properties, reducing the data that is sent over the wire. Or you could add extra properties for a specific use that are not part of the domain entity, such as a calculated value.
Projections can also be used to update a subset of properties on a domain entity. This is useful when you only want to update one or two properties and not save the entire entity.
There are also two types of projections in SDN: interface-based and DTO-based. Interface-based projections are useful when you want to create a projection that contains a subset of properties from a domain entity. DTO-based projections can be more flexible by using a custom query to create a projection with additional properties or updated values.
Lastly, Spring Data Neo4j supports multi-level projections, where you can create a projection that contains another projection (nested projection). This is useful when you want a subset of properties from a domain entity, but also a subset of a related entity.
More information on projections is available in the projections section of the Spring Data Neo4j documentation. The next optional lessons will look at many of these scenarios in more detail.
Check Your Understanding
Use Case for Projections
What is the use case for a projection (both interface-based and DTO-based)?
-
❏ Sending more data over the wire.
-
❏ Creating lots of new domain entities.
-
✓ Reducing database load for entities containing lots of properties that may not always be needed.
Hint
Projections allow you to create customized versions of a large object, reducing database load for entities containing lots of properties.
Solution
Projections can help reduce database load for entities containing lots of properties that may not always be needed.
Lesson Summary
In this lesson, you learned how projections are customized versions of domain entities, allowing you to splice and dice large objects into smaller ones to make them easier to work with. Projections can be interface-based or DTO-based.
In the next optional lesson, you will learn how to create an interface-based projection for the Movie
entity.