Finding an Entity
The find method is a handy way of retrieving a single object stored in Onyx Database. This tutorial assumes that you have already stored a hierarchy of related data in an embedded local Onyx Database. In this case, the data stores the stats for all of the team leaders for each team in the NFL for the 2015 season. To see how the data was seeded for this example, see Main.java.
Steps to Find an Entity (Onyx Cloud Database)
1
Create a New Entity Instance with the Identifier Set
Make sure to set the identifier field so that the manager knows which record to find.
1const league = { name: "NFL" }; // set the id
2await db.save('League', league);
- The entity is just an empty shell.
- Ensure the identifier field is set.
2
Invoke the find Method
This will retrieve the entity object you're looking for.
1const foundLeague = await db.findById('League', league.name);
- If the resource you are requesting does not exist, a
NoResultsException
is thrown.
3
Confirm That the Other Fields of the Entity Were Populated
1console.log(`The description of the league that was found is: '${foundLeague.description}'`);
Important Notes
- The
find
method requires the identifier field to be set on the entity before invocation. - If the entity does not exist in the database, a
NoResultsException
will be thrown.
Next Steps
Now that you know how to find an entity, you can explore advanced querying techniques: