V
V
Vitaly Khvan2021-08-05 07:46:43
Java
Vitaly Khvan, 2021-08-05 07:46:43

How to set up dynamic query in Jpa repository?

Good afternoon everyone,
there is a repository of the form

@NoRepositoryBean
public interface AbstractRepository<E extends AbstractEntity> extends JpaRepository<E, Integer> {
    Boolean existsByHash(String hash);

    E findByHash(String hash);
}


How to approximately write a query like,
Boolean existsBy("tableName"/"entityName", "fieldName", "fieldValue")

The whole point is, I have annotations for validating incoming data, and it is necessary to check fields in requestEntity (more than 20 different ) for uniqueness or presence in the database before attempting to save the data.

The only problem is writing a proper abstract repository so that all descendants have the correct existsBy() method;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-08-05
@xRites

For such tasks, it is better to use the Criteria API.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question