简体   繁体   中英

How to prevent JPA query including a String constructor, throwing a NullPointerException when a null String value is only availabe

I have the following JPA query: select NEW java.lang.String(c.country) from Contact c group by c.country order by c.country

When one of the rows in Contact table holds null value for country field, the String constructor in the JPA query above throws a NullPointerException.

As a null value in the country field in Contact table is allowed, is there any way to prevent it via code and still use a named query?

关于什么

select c.country from Contact c where c.country is not null group by c.country order by c.country

A group by makes no sense here, since you don't have any aggregate function in the select clause. Calling the string constructor is also useless.

Just use

select c.country from Contact c order by c.country

If the goal of the group by is to select distinct values, use distinct :

select distinct c.country from Contact c order by c.country

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM