Ich folge diesen Beiträgen (https://www.thoughts-on-java.org/persist-localdate-localdatetime-jpa/, https://github.com/lbtc-xxx/jpa21converter), um Konverter von JPA 2.1 sql.Timestamp zu LocalDateTime und umgekehrt zu implementieren.JPA AttributeConverter für LocalDateTime
Allerdings sieht es so aus, als ob mein Konventer nicht funktioniert. Es scheint, dass es nie aufgerufen wird, da sysout nichts ausgibt.
Dies ist mein Konverter:
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());
}
}
Diese
Entitätsklasse ist@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;
...
}
Das ist meine Methode im Mai DAO-Klasse ist:
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();
Dies gibt:
1|hello|null
2|hi|null
3|howdy|null
Vielen Dank im Voraus.
Ihre Frage enthält nicht einmal das Wort 'transient' ... –
Ja. Ich habe es verpasst. –
Wie erwarten Sie, dass Ihnen jemand hilft, wenn Sie den eigentlichen Code nicht veröffentlichen? –