What is the best way / best practice to check if an entity is already in a database using JPA?
I am writing a client that adds hostinformation to a db. For example the attached storage luns, hba etc...
If I want to add a Lun for a host, I have to check if the lun is already in the database. (The Lun can be attached to another host already).
I see 2 possibilites:
Does someone have experience with that.
BR, Rene
entityManager.find(SomeEntity.class, id)
Returns: the found entity instance or null if the entity does not exist
This will do a simple select in the DB. Just make sure your collections (if any) are lazy.
I think checking for the unique constraint exception is a better way. Think about the cost for every insert...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With