spring-projects / spring-projects/spring-data-relational

Projections require Entity Id field to not throw exception while instantiating

Open
#2,088 2 comments 0 reactions 1 assignee View on GitHub

@schauder is already working on this.

Since Jul 21, 2025.

status: feedback-provided status: waiting-for-triage
Dominant language
Java
Stars
827
Forks
394
PR merge metrics
No merged PRs in 30d

Description

Issue Description

When creating interface projections that only expose specific fields (closed projections), omitting the entity's @Id field can result in runtime errors during proxy creation, even when the projected fields contain valid non-null data.

Entity
@Data
@Builder
@Table("file")
public class File {
    @Id
    private Long id;
    private String fileId;
    private Long uploaderId;
    private LocalDateTime uploadDate;
    private String fullName;
    @Builder.Default
    @MappedCollection(idColumn = "file_id")
    private Set<Document> documents = new HashSet<>();
}
Projection
public interface FilePreview {
    String getFileId();
    LocalDateTime getUploadDate();
}
Repository
public interface FileRepository extends CrudRepository<File, Long> {
    List<FilePreview> findBy();
}

When invoking findBy an java.lang.IllegalStateException is thrown with message Identifier value must not be null at this point.

Modifying the projection to

public interface FilePreview {
    Long getId();
    String getFileId();
    LocalDateTime getUploadDate();
}

results in the method not throwing exception.

The documentation does not mention anywhere this as an intended behaviour and it's not clear why would it be needed.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.