Uploaded image for project: 'Spring Data JPA'
  1. Spring Data JPA
  2. DATAJPA-1301

Map instances returned by JpaRepository queries do not handle null map values correctly

    Details

      Description

      Given the JpARepository:

      import org.springframework.data.jpa.repository.JpaRepository;
      import org.springframework.data.jpa.repository.Query;
      import org.springframework.stereotype.Repository;
      
      import java.util.Map;
      
      @Repository
      interface MyEntityJpaRepo extends JpaRepository<MyEntity, String> {
      
          @Query("SELECT t.id as id, t.foo as foo, t.bar as bar FROM MyEntity t WHERE t.id = ?1")
          Map findSomething(String id);
      
      }
      
      

      And table data:

      id foo bar
      "1" "x" null

      The Map instance returned does not behave correctly.

      // Pseudocode
      Map map = myEntityJpaRepo.findSomething("1");
      
      // the following are true
      map == { "id": "1", "foo": "x", "bar": null }
      map.keySet() == { "id", "foo", "bar" }
      
      
      map.get("id").equals("1")   // true
      map.get("foo").equals("x")  // true
      map.get("bar") == null      // true
      
      map.containsKey("id") == true
      map.keySet().contains("id") == true
      map.containsKey("foo") == true
      map.keySet().contains("foo") == true
      
      // the following should be true, but are false
      map.containsKey("bar") == true
      map.keySet().contains("bar") == true
      

      It looks like containsKey method is not implemented correctly on AbstractJpaQuery.

        Attachments

          Activity

            People

            • Assignee:
              schauder Jens Schauder
              Reporter:
              sparrowlegs John Mann
              Last updater:
              Mark Paluch
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: