-3
我正在密切關注這些帖子(https://www.thoughts-on-java.org/persist-localdate-localdatetime-jpa/,https://github.com/lbtc-xxx/jpa21converter)以實現從JPA 2.1 sql.Timestamp到LocalDateTime的轉換器,反之亦然。LocalDateTime的JPA AttributeConverter
但是,它看起來像我的轉檯不起作用。它似乎從來沒有被稱爲sysout不打印任何東西。
這是我的轉換器:
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());
}
}
這是實體類
@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;
...
}
這是我在5月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();
這會打印出:
1|hello|null
2|hi|null
3|howdy|null
預先感謝您。
你的問題甚至沒有包含「瞬態」這個詞...... –
是的。我錯過了它。 –
如果你沒有發佈實際的代碼,你期望有人能幫助你嗎? –