Blazebit / blaze-persistence

Rich Criteria API for JPA providers
https://persistence.blazebit.com
Apache License 2.0
742 stars 90 forks source link

Queryable.getSingleResult alternatives #1934

Open haraleib opened 2 months ago

haraleib commented 2 months ago

It would be nice if com.blazebit.persistence.Queryable would offer alternative Methods for getSingleResult. In fact a Method which swallows NoResultsException and NonUniqueResultException. A proper Naming would be findSingleResult. As this two Exceptions are unchecked Exceptions, very often it is not very safe to use getSingleResult, unless you really know your data.

Maybe instead of findSingleResult a Method which returns an Optional could be a good solution too.

criteriaBuilderFactory.create(em, CatView.java)
            .where("id").eq(catId)
            .findSingleResult();
criteriaBuilderFactory.create(em, CatView.java)
            .where("id").eq(catId)
            .getSingleResultOptional();