Skip to content

Projections require Entity Id field to not throw exception while instantiating #2088

@VariabileAleatoria

Description

@VariabileAleatoria

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 id;
    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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions