Je suis de près ces messages (https://www.thoughts-on-java.org/persist-localdate-localdatetime-jpa/, https://github.com/lbtc-xxx/jpa21converter) pour implémenter le convertisseur de JPA 2,1 sql.Timestamp à LocalDateTime et vice-versa.Attribut JPA AttributeConverter pour LocalDateTime
Cependant, il semble que mon conveter ne fonctionne pas. Il semble qu'il ne soit jamais appelé car sysout n'imprime rien.
Ceci est mon convertisseur:
import java.sql.Timestamp;
import java.time.LocalDateTime;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter(autoApply = true)
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp>{
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
System.out.println("CONVERTER TIMESTAMPS: " + localDateTime);
return (localDateTime == null ? null : Timestamp.valueOf(localDateTime));
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp timestamp) {
System.out.println("CONVERTER TIMESTAMPS: " + timestamp);
return (timestamp == null ? null : timestamp.toLocalDateTime());
}
}
C'est classe d'entité
@Entity
@Table(name = "survey")
public class Question {
@Id
@GeneratedValue(strategy = Generationtype.AUTO)
private Integer id;
...
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
/*I also tried here: @Convert(converter = LocalDateTimeConverter.class))*/
private LocalDateTime createdAt;
...
}
Ceci est ma méthode en classe peut DAO:
TypedQuery<Question> query = em.createQuery("SELECT q FROM Question q", Question.class);
query.getResultList().forEach(question -> {
LOGGER.info("{}|{}|{}", question.getId(), question.getDescription(), question.getCreatedAt());
});
return query.getResultList();
Ceci affiche:
1|hello|null
2|hi|null
3|howdy|null
Merci d'avance.
Votre question ne contient même pas le mot «transitoire» ... –
Ouais. Je l'ai raté. –
Comment voulez-vous que quelqu'un vous aide si vous ne postez pas le code? –