Here is a self-contained sample how it should work:
package test;
import javax.persistence.*;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
public class MyTestCase {
public static void main(String[] args) {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory(
"objectdb:$objectdb/db/test.tmp;drop");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
MyEntity e = new MyEntity();
e.setCreated(LocalDateTime.now());
em.persist(e);
em.getTransaction().commit();
Query query = em.createQuery("SELECT e FROM MyEntity e");
List<MyEntity> resultList = query.getResultList();
System.out.println(resultList.get(0).getCreated());
em.close();
emf.close();
}
@Entity
public static class MyEntity {
@Id
private Long id;
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime created;
protected MyEntity() {}
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public LocalDateTime getCreated() { return created; }
public void setCreated(LocalDateTime created) { this.created = created; }
}
@Converter(autoApply = true)
public static class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public java.sql.Timestamp convertToDatabaseColumn(java.time.LocalDateTime attribute) {
return attribute == null ? null : java.sql.Timestamp.valueOf(attribute);
}
@Override
public java.time.LocalDateTime convertToEntityAttribute(java.sql.Timestamp dbData) {
return dbData == null ? null : dbData.toLocalDateTime();
}
}
}
Of course, as ObjectDB doesn't recognize the @Convert annotation defined in the JPA standard, I get an rollback caused by this exception:
com.objectdb.o.UserException: Attempt to store an instance of a non persistable type java.time.LocalDateTime - field test.MyTestCase$MyEntity.created