I am trying to add to my project (use Spring 5.0.2, Spring-data-jpa 2.0.5 and Hibernate 5.2.14) a new annotation that allows me to obtain the date directly from the database (Postgresql) instead of the VM.
For this, I have taken as my base the following article .
The problem is that the INSERT that Hibernate is trying to do in the database and that is displayed by the console, does not include current_date in the field that it would touch, but tries to insert a null value and, being a PK and do not admit the null value, a restriction exception is violated.
In summary, it seems that you are not invoking the new annotation that I have created and with which I decorate the corresponding attribute, do I need to configure something else? Do I have to add another previous annotation?
My new entry DataBaseDate:
@ValueGenerationType(generatedBy = DataBaseDateGenerator.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataBaseDate {
GenerationTiming getGenerationTiming() default GenerationTiming.ALWAYS;
}
My DataBaseDateGenerator class:
public class DataBaseDateGenerator implements AnnotationValueGeneration<DataBaseDate> {
private static final long serialVersionUID = 1L;
@Override
public GenerationTiming getGenerationTiming() {
return GenerationTiming.ALWAYS;
}
@Override
public ValueGenerator<?> getValueGenerator() {
// no in-memory generation
return null;
}
@Override
public boolean referenceColumnInSql() {
return true;
}
@Override
public String getDatabaseGeneratedReferencedColumnValue() {
return "current_date";
}
@Override
public void initialize(final DataBaseDate annotation, final Class<?> propertyType) {
// do nothing
}
}
The class that defines the Embeddable id:
@Embeddable
public class ComplexJPAPK implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "id_anuncio", updatable = false, nullable = false)
private Integer idAnuncio;
@Column(name = "fecha_alta", updatable = false, nullable = false)
@DataBaseDate(getGenerationTiming = GenerationTiming.INSERT)
private LocalDate fechaAlta;
public ComplexJPAPK() {
super();
}
// getters/setters omitidos para simplificar la lectura
}
The class that defines the EmbeddedId:
@Entity
public class ComplexJPA {
@EmbeddedId
private ComplexJPAPK id;
private String codArticulo = StringUtils.EMPTY;
public ComplexJPA() {
super();
}
// getters/setters omitidos para simplificar la lectura
}
The class that contains ComplexJPA:
@Entity
@Table(name="anuncio")
public class AnuncioJPA {
@OneToMany(mappedBy = "anuncio", cascade = { CascadeType.PERSIST,
CascadeType.MERGE }, orphanRemoval = false, fetch = FetchType.LAZY)
private final List<ComplexJPA> complexJPAList= new ArrayList<>();
// Resto de elementos omitidos para simplificar la lectura
public AnuncioJPA() {
super();
}
}
The SQL code that is displayed by console when trying to "save" the object and where pre-guess should show the function current_date instead of waiting for a parameter:
Hibernate: insert into complexJPA(responsable, fecha_alta, anuncio_anuncio_id) values (?, ?, ?)
In any case, I have omitted the code to add the ComplexJPA element to the APJPA class and the call to saveAndFlush () in its repository, because if I initialize the date (with LocalDate.now ()), it works correctly, so I assume that the problem does not come from there.