public < T , Y > T getUniqueByAttribute ( Class < T > entityCls , String attributeName , Y value ) { if ( entityCls == null ) { throw new IllegalArgumentException ( "entityCls cannot be null" ) ; } if ( attributeName == null ) { throw new IllegalArgumentException ( "attributeName cannot be null" ) ; } TypedQuery < T > query = createTypedQuery ( entityCls , attributeName , value ) ; T result = null ; try { result = query . getSingleResult ( ) ; } catch ( NonUniqueResultException nure ) { LOGGER . warn ( "Result not unique for {}: {} = {}" , entityCls . getName ( ) , attributeName , value ) ; result = query . getResultList ( ) . get ( 0 ) ; } catch ( NoResultException nre ) { LOGGER . debug ( "Result not existant for {}: {} = {}" , entityCls . getName ( ) , attributeName , value ) ; } return result ; }
Executes a query for the entity with the given attribute value . This method assumes that at most one instance of the given entity will be a match . This typically is used with attributes with a uniqueness constraint .