Skip to content

SimpleJpaRepository.delete does not respect JPA @Access rules [DATAJPA-1565] #1854

@spring-projects-issues

Description

@spring-projects-issues

Leonard Brünings opened DATAJPA-1565 and commented

With the changes of DATAJPA-1535 (c1d99ae) a bug was introduced.

According to JPA rules the position of the annotation defines whether field or property access is used. It can be overridden with the @Access annotation. It seems that org.springframework.data.jpa.repository.support.JpaEntityInformation does not respect that since entityInformation.getId(entity) uses DirectFieldAccessFallbackBeanWrapper which tries the property access first. This can lead to problems if the getter returns a different type than the type of the actual field.

@Entity
public class ExampleEntity {

    @Id
    @Column(columnDefinition = "uuid")
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    private UUID clientId;

    public UUID getId() {
        return clientId;
    }

    public void setClientId(UUID clientId) {
        this.clientId = clientId;
    }

    public String getClientId() {
        return clientId == null ? null : clientId.toString();
    }
}

Fails with (see attached sample)

org.springframework.dao.InvalidDataAccessApiUsageException: Provided id of the wrong type for class com.example.datajpaidaccessbug.db.ExampleEntity. Expected: class java.util.UUID, got class java.lang.String; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class com.example.datajpaidaccessbug.db.ExampleEntity. Expected: class java.util.UUID, got class java.lang.String
 at com.example.datajpaidaccessbug.DatajpaIdAccessBugTests.deletionFails(DatajpaIdAccessBugTests.java:22)

Affects: 2.1.9 (Lovelace SR9)

Attachments:

Issue Links:

  • DATAJPA-1576 JpaMetamodelEntityInformation.getId(…) uses field access for proxied entities
    ("is duplicated by")

Metadata

Metadata

Labels

in: query-parserEverything related to parsing JPQL or SQLtype: bugA general bug

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions