How to check if an entity exists in a Database without loading it?

#1

Hi,

I am trying to speed up some code - where i check if an entity exists in order to make a decision.

In Java - It looks like this (pk - is the primary key of the entity):

MyClass find  = em.find(MyClass.class, pk);

boolean exists = find != null;

Is there a way to check the existence without loading the entity?

Thanks

 

#2

You can use a query:

SELECT e.pk FROM MyClass e WHERE e.pk = :pk

or

SELECT COUNT(e) FROM MyClass e WHERE e.pk = :pk

 

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login