How to make an optional/nullable property in Hibernate/JPA

48
July 16, 2017, at 9:00 PM

I found this implementation that basically do what I want.

@ConstraintComposition(OR)
@NotBlank
@Null
@ReportAsSingleViolation
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Constraint(validatedBy = {})
public @interface NullOrNotBlank {
    String message() default "{org.hibernate.validator.constraints.NullOrNotBlank.message}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

and I use it as

@field:NullOrNotBlank
@field:Size(max = 50)
var middleName: String? = null,

I've also tried

@field:NullOrNotBlank
@field:Size(max = 50)
@field:Column(nullable = true)
var middleName: String? = null,

But this composite constraints makes the field in the database (MySQL) not nullable. So what happens is that it's working fine until I save the Entity to the database.

READ ALSO
Every time open app , open last open activity

Every time open app , open last open activity

Sir, The last opened activity will be saved and when opening the app, the last opened activity will be openedBut app reopen every time then see splash screen then go last open activity, Please help me for it

35
Run Spring boot .jar file with spring.config.location with replace values of application.properties?

Run Spring boot .jar file with spring.config.location with replace values of application.properties?

When I run a Spring boot application from command line with --springconfig

53