Deleting an Entity
Deleting data is a fundamental operation in any database system. This tutorial demonstrates how to delete a single entity using Onyx Database.
Steps to Delete an Entity (Onyx Cloud Database)
1
Save an Entity
Create and save an entity that you will later delete.
1val person = Person(
2 firstName = "John",
3 lastName = "Elway",
4 dateCreated = Date()
5)
6
7val savedPerson = this.save(person)
8println("Person ${savedPerson.id} saved successfully")2
Delete the Entity
Use the Persistence Manager's delete method to remove the entity.
1this.delete(savedPerson)3
Verify the Deletion
Attempt to retrieve the deleted entity to confirm it no longer exists.
1val deletedPerson = this.findById<Person>(savedPerson.id)
2if (deletedPerson == null) {
3 println("Entity was deleted successfully")
4}Important Notes
- The delete operation is an atomic operation. Ensure that the entity's primary key is correctly configured.
- Onyx Database does not differentiate between soft deletes and hard deletes. Use an appropriate deletion strategy based on your requirements.
Troubleshooting
- Entity Not Found: Verify that the correct identifier was provided for the entity being deleted.
- Deletion Failed: Ensure the entity exists and the Persistence Manager is properly initialized.
- Unexpected Exceptions: Check the logs for error details, such as database connection issues or constraint violations.
Next Steps
Once you have mastered deleting entities, explore related topics: