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

Giving the JpaRepository an invalid type as an ID also works. #2759

Closed
heowc opened this issue Jan 10, 2023 · 4 comments
Closed

Giving the JpaRepository an invalid type as an ID also works. #2759

heowc opened this issue Jan 10, 2023 · 4 comments
Assignees
Labels
status: pending-design-work Needs design work before any code can be developed

Comments

@heowc
Copy link
Contributor

heowc commented Jan 10, 2023

For example, Post's @Id is Long.
However, the application runs normally even if it is written as JpaRepository<Post, String>.

application

@Entity
public class Post {

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

    private String title;

    private LocalDateTime createdAt;

    // ...
}
public interface PostRepository extends JpaRepository<Post, String> {
}

test

@DataJpaTest
class PostRepositoryTest {

    @Autowired
    private PostRepository repository;

    @Test
    void test() {
        Optional<Post> post = repository.findById("1");
        assertThat(post).isEmpty();
    }
}

log


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.1)

2023-01-11T12:27:55.850+09:00  INFO 10870 --- [    Test worker] c.e.springdatajpabug.PostRepositoryTest  : Starting PostRepositoryTest using Java 17.0.4 with PID 10870 (started by nhn in /Users/nhn/Project/wooteco5/spring-data-jpa-bug)
2023-01-11T12:27:55.851+09:00  INFO 10870 --- [    Test worker] c.e.springdatajpabug.PostRepositoryTest  : No active profile set, falling back to 1 default profile: "default"
2023-01-11T12:27:55.981+09:00  INFO 10870 --- [    Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-01-11T12:27:56.002+09:00  INFO 10870 --- [    Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 17 ms. Found 1 JPA repository interfaces.
2023-01-11T12:27:56.025+09:00  INFO 10870 --- [    Test worker] beddedDataSourceBeanFactoryPostProcessor : Replacing 'dataSource' DataSource bean with embedded version
2023-01-11T12:27:56.088+09:00  INFO 10870 --- [    Test worker] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:h2:mem:09fee617-9b51-41cb-ba55-f7f7f521891c;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
2023-01-11T12:27:56.229+09:00  INFO 10870 --- [    Test worker] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-01-11T12:27:56.255+09:00  INFO 10870 --- [    Test worker] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.1.6.Final
2023-01-11T12:27:56.342+09:00  WARN 10870 --- [    Test worker] org.hibernate.orm.deprecation            : HHH90000021: Encountered deprecated setting [javax.persistence.sharedCache.mode], use [jakarta.persistence.sharedCache.mode] instead
2023-01-11T12:27:56.411+09:00  INFO 10870 --- [    Test worker] SQL dialect                              : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Hibernate: drop table if exists post cascade 
Hibernate: create table post (id bigint generated by default as identity, created_at timestamp(6), title varchar(255), primary key (id))
2023-01-11T12:27:56.722+09:00  INFO 10870 --- [    Test worker] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-01-11T12:27:56.726+09:00  INFO 10870 --- [    Test worker] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-01-11T12:27:56.860+09:00  INFO 10870 --- [    Test worker] c.e.springdatajpabug.PostRepositoryTest  : Started PostRepositoryTest in 1.136 seconds (process running for 1.653)
Hibernate: select p1_0.id,p1_0.created_at,p1_0.title from post p1_0 where p1_0.id=?
2023-01-11T12:27:56.994+09:00  INFO 10870 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2023-01-11T12:27:56.994+09:00  INFO 10870 --- [ionShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
Hibernate: drop table if exists post cascade 

In short, I think it is necessary to verify this in the place where RepositoryMetadata is implemented. Any better ideas? 🤔

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 10, 2023
@heowc heowc changed the title Giving the JpaRepository an invalid type as an id also works. Giving the JpaRepository an invalid type as an ID also works. Jan 11, 2023
@christophstrobl christophstrobl added the for: team-attention An issue we need to discuss as a team to make progress label Jan 16, 2023
@christophstrobl
Copy link
Member

potentially related to: spring-projects/spring-data-commons#1458

@christophstrobl christophstrobl added status: pending-design-work Needs design work before any code can be developed and removed status: waiting-for-triage An issue we've not yet triaged for: team-attention An issue we need to discuss as a team to make progress labels Feb 27, 2023
@schauder
Copy link
Contributor

This is actually used in some stores as a feature to support composite ids for which a Map is used as id in the repository.

We could log a warning or we could make this module dependent so that stores that don't need this throw an exception.

@igorwojda
Copy link

May be also related to #2840

@mp911de
Copy link
Member

mp911de commented Jan 31, 2025

Repository generics are primarily a user-facing API contract as the implementation can work with Object primary keys and we do not have means to verify the declared generics vs the identifier type that is being passed into the repo calls. This isn't really a problem that we want to solve. It could be addressed much better on the tooling side like IDE's verifying the object identifier type vs the repository declaration.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: pending-design-work Needs design work before any code can be developed
Projects
None yet
Development

No branches or pull requests

6 participants