0
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));

By using the statement joinAddress.get(Address_.ADDRESS_ID) above inside multiselect, I am trying to select the foreign key of the table Address in Location table. As i am doing left join, for some row it is coming as null. So when I am trying to select it is showing the illegal argument exception.

Ruby Kannan
  • 251
  • 1
  • 6

1 Answers1

0

I have solved this problem by adding cases to check whether the column is null or not null.

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);
Ruby Kannan
  • 251
  • 1
  • 6