Skip to content

Rule violation reported for non nullable JPA property where @NotNull annotation is part of applied custom validation constraint #21

@MartinAhrer

Description

@MartinAhrer

The following custom validation constraint has been applied to a JPA property

@MustBeDocumented
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
@NotBlank
@NotNull
@Size(max = 10)
annotation class ApplicantIdConstraint
@Entity
@EntityListeners(AuditingEntityListener::class)
class Application(
    //@field:NotNull // TODO should not be necessary but required to pass the clean code rules
    @field:ManyToOne(optional = false)
    val user:User,
    @field:ApplicantIdConstraint
    //@field:NotNull // TODO should not be necessary but required to pass the clean code rules
    val applicantId: String,
): AbstractPersistable<Long>() {
    @Suppress("UnusedPrivateMember")
    @field:CreatedDate
    private var createdAt: Instant? = null
}

Only after adding the @field:NotNull annotation the rule violation is no longer reported.
But this should not be necessary as:

  1. annotation ApplicantIdConstraint already enforces non-nullability
  2. annotation ManyToOne(optional = false) implies non-nullability. See https://docs.oracle.com/javaee/6/api/javax/persistence/ManyToOne.html#optional()

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions