当外键列为null时,jpa条件中的multiselect引发非法参数异常

时间:2019-05-16 14:07:32

标签: jpa spring-data-jpa jpa-criteria

Join<Location, LocationType> locationTypeJoin = root.join(Location_.LOCATION_TYPE);
        Join<Location, Address> addressJoin = root.join(Location_.ADDRESS, JoinType.LEFT);
        Join<Address, State> stateJoin = addressJoin.join(Address_.STATE, JoinType.LEFT);
        Join<Address, County> countyJoin = addressJoin.join(Address_.COUNTY, JoinType.LEFT);

criteriaQuery.multiselect(
            root.get(Location_.LOCATION_ID), root.get(Location_.LOCATION_NAME), root.get(Location_.LOCATION_CODE),
            root.get(Location_.LAST_UPDATE_DATE_TIME), root.get(Location_.CREATE_DATE_TIME), root.get(Location_.SOURCE_SYSTEM_UNIQUE_KEY),
            joinLocationType.get(LocationType_.LOCATION_TYPE_ID), joinLocationType.get(LocationType_.LOCATION_TYPE),
            joinLocationType.get(LocationType_.LOCATION_TYPE_DESC),joinAddress.get(Address_.ADDRESS_ID));

通过在multiselect中使用上面的语句joinAddress.get(Address_.ADDRESS_ID),我试图在Location表中选择表Address的外键。当我在做左连接时,对于某行它为null。因此,当我尝试选择它时,它将显示非法的参数异常。

1 个答案:

答案 0 :(得分:0)

我已经通过添加案例以检查列是否为空来解决此问题。

Expression<Object> case1 = criteriaBuilder.selectCase().when(criteriaBuilder.isNotNull(joinAddress.get(Address_.ADDRESS_ID)), joinAddress.get(Address_.ADDRESS_ID)).otherwise(0);
criteriaQuery.multiselect(
        root.get(Location_.LOCATION_ID), root.get(Location_.LOCATION_NAME), root.get(Location_.LOCATION_CODE),
        root.get(Location_.LAST_UPDATE_DATE_TIME), root.get(Location_.CREATE_DATE_TIME), root.get(Location_.SOURCE_SYSTEM_UNIQUE_KEY),
        joinLocationType.get(LocationType_.LOCATION_TYPE_ID), joinLocationType.get(LocationType_.LOCATION_TYPE),
        joinLocationType.get(LocationType_.LOCATION_TYPE_DESC),case1);