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:
- annotation
ApplicantIdConstraint already enforces non-nullability
- annotation
ManyToOne(optional = false) implies non-nullability. See https://docs.oracle.com/javaee/6/api/javax/persistence/ManyToOne.html#optional()