Skip to content

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

@heowc

Description

@heowc

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? 🤔

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions