Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map instances returned by JpaRepository queries do not handle null map values correctly [DATAJPA-1301] #1625

Closed
spring-projects-issues opened this issue Mar 23, 2018 · 5 comments
Assignees
Labels
type: bug A general bug

Comments

@spring-projects-issues
Copy link

John Mann opened DATAJPA-1301 and commented

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


Affects: 2.1 M1 (Lovelace), 1.11.10 (Ingalls SR10), 2.0.5 (Kay SR5)

Referenced from: pull request #262

Backported to: 2.0.6 (Kay SR6), 1.11.11 (Ingalls SR11)

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

I am not sure we can actually completely fix this due to Hibernate's refined alias handling being aligned with SQL which assumes all caps aliases by default. Depending on the database you're using you can explicitly quote the aliases in the query which should make them appear in exactly the case you defined it.

Jens Schauder – Would you mind having a look at this? Maybe we can tweak the implementation slightly?

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

That's polished, merged and backported into Kay and Ingalls. Feel free to give the snapshots a try

@spring-projects-issues
Copy link
Author

John Mann commented

This is still broken in 2.0.1.BUILD-SNAPSHOT. The containsKey method is still returning false for map entries with null values

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

That's expected as 2.0.5.BUILD-SNAPSHOT is the version that the fix was applied to. Looks like you mix up Spring Boot versions with Spring Data ones?

@spring-projects-issues
Copy link
Author

John Mann commented

Ah, crap. I'm sorry. I will retest with the correct version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants