Java JPA: perforant checks whether the entity is already in the DB

What is the best method / best practice to use JPA to check whether an entity is already in the database?

I'm writing a client that adds host information to the database For example, additional storage containers, HbA, etc

If I want to add Lun to the host, I must check whether the Lun is already in the database (Lun can be attached to another host)

I see two possibilities:

>I select Lun, check if it is already in the database > try to insert Lun and check for exceptions (unique constraints)

Does anyone have experience?

BR,Rene

Solution

entityManager.find(SomeEntity.class,id)
entityManager.find(SomeEntity.class,id)

This will make a simple selection in the DB Just make sure your collection (if any) is lazy

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>