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

Incorrect implementation of "ID" in SQL queries #3173

Closed
olech2412 opened this issue Oct 1, 2024 · 2 comments
Closed

Incorrect implementation of "ID" in SQL queries #3173

olech2412 opened this issue Oct 1, 2024 · 2 comments
Assignees
Labels
type: documentation A documentation update

Comments

@olech2412
Copy link

We have the following behavior.

There is a class that is structured like this:

@Entity
@Table(name = "my_entity")
public class MyEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long pk;

    private Long id;
    private String name;

    public MyEntity() {
    }
}

The corresponding repository looks like this and contains a function "existsById":

@Repository
public interface MyEntityRepository extends CrudRepository<MyEntity, Long> {

    boolean existsById(Long id);

    boolean existsByIdAndName(Long id, String name);
}

Suppose the database contains the following data record:

pk🔑 id name
1 204 Name1
2 2830 Name2
3 52235 Name3

In our case the following behavior occurred:

existsById(204L) -> false

existsById(2L) -> true

existsByIdAndName(204L, "Name1") -> true

The behavior that was expected was:

The identifier Id in the function name "existsById" refers exclusively to the attribute of the class and is not internally reserved for the primary key. Accordingly, every query should only target this attribute without exception.

SpringBoot-Starter-Data-JPA-Version: 3.3.3

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 1, 2024
@mp911de mp911de transferred this issue from spring-projects/spring-data-jpa Oct 8, 2024
@mp911de mp911de added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 8, 2024
@christophstrobl
Copy link
Member

Thank you @olech2412 for getting in touch. This is a topic that has been discussed when working on the naming proposed via #1399 and in particular this comment.

Looking at the reference documentation I do see a need to be more clear about predefined method names and strategies to cope with the described scenario. I might be wrong, but it seems only the Repository Core Concepts briefly touches upon this topic in one of the snippets.

@olech2412
Copy link
Author

Hi @christophstrobl , thanks for the answer. I also think it would make sense if this were better documented. We then thought about what was happening, but we couldn't estimate whether this was a bug or at what level in the data layer the cause lay. The conflict probably doesn't occur very often, but in the situations where it does happen it is probably even more serious. Thank you for your effort! 🚀

mp911de pushed a commit that referenced this issue Oct 17, 2024
Add section on reserved method names within repository interfaces.

Closes #3173
Original pull request: #3174
mp911de added a commit that referenced this issue Oct 17, 2024
Add DDD context. Explain identifier to domain object relationship. Tweak wording.

See #3173
Original pull request: #3174
mp911de pushed a commit that referenced this issue Oct 17, 2024
Add section on reserved method names within repository interfaces.

Closes #3173
Original pull request: #3174
mp911de added a commit that referenced this issue Oct 17, 2024
Add DDD context. Explain identifier to domain object relationship. Tweak wording.

See #3173
Original pull request: #3174
mp911de added a commit that referenced this issue Oct 17, 2024
Add DDD context. Explain identifier to domain object relationship. Tweak wording.

See #3173
Original pull request: #3174
@mp911de mp911de added this to the 3.2.11 (2023.1.11) milestone Oct 17, 2024
@mp911de mp911de linked a pull request Oct 17, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants