Issue #1495: java 8 LocalDateTime is not working in query

Type: Bug ReoprtVersion: 1.4.0Priority: NormalStatus: FixedReplies: 12
#1

My entity class has a field of type java.time.LocalDateTime. I'm able to persist entities and I'm able to query without datetime field in where clause, but with datetime in where clause doesn't return anything and no exception.

 

    EntityManager em = getInstance().getEntityManger();
    Query query = em.createQuery(
        "SELECT t FROM Test t WHERE t.date BETWEEN :startDate AND :endDate ORDER BY t.date");
    query.setParameter("startDate", LocalDateTime.now());
    query.setParameter("endDate", LocalDateTime.now().plusDays(1));

above code returns empty list

 

but it's working fine:

SELECT t FROM Test t
#2

New Java 8 date/time types are not supported by JPA yet.

ObjectDB Support
#3

I don't want to sound rude, but if you want to play the JPA compatibility card here, it is not quite working: In JPA you can use the @Convert annotation in order to translate a value, which is at least a workaround. But ObjectDB currently doesn't support @Convert ( http://www.objectdb.com/database/forum/849 ). So we are between a rock and a hard place because on the one side ObjectDB is too compatible, and on the other side it is not compatible enough.

I would appreciate any constructive thoughts concerning this problem.

#4

You are right. If you can provide a small single top level class example (in this form), which shows how you can use Java 8 date/time using @Convert, we may be able to add this support.

ObjectDB Support
#5

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

 

 

#6

@Convert is working for me with the latest version of ObjectDB:

---------------

@Convert(converter = LocalDateTimeConverter.class)
    private Timestamp startTime;
    @Convert(converter = LocalDateTimeConverter.class)
//note it's not working with objectdb which supports jpa 2.0 not 2.1, using Convert
    private Timestamp endTime;
------------------

@Converter
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime,Timestamp> {

    @Override
    public Timestamp convertToDatabaseColumn(LocalDateTime dateTime) {
        return Timestamp.valueOf(dateTime);
    }

    @Override
    public LocalDateTime convertToEntityAttribute(Timestamp timestamp) {
        return timestamp.toLocalDateTime();
    }
}

 

#7

@Convert is not implemented yet by ObjectDB.

However, the test at #5 above passes if in the configuration serialization is enabled:

    <persist serialization="true" />

This could be the reason, Joe, that your code works, and possibly enabling serialization could be also a solution for you Landei. It is a limited solution, however, since indexing such a field is not supported.

ObjectDB Support
#8

I am not explicitly using objectdb.conf and hence default serialization="false" is being used in my case.

What could be the other reason it's still working for me? my entity class is Serializable though.

I'm using objectdb 2.2.5.

#9

Actually in #6 above the field is defined as java.sql.Timestamp, which is a JPA supported type, so serialization is not needed. @Convert is ignored by ObjectDB.

The code in #5 works with serialization enabled and fails with serialization disabled.

ObjectDB Support
#10

You are right, now I remember @Convert was actually not working but I left it there as it is. I'm actually manually converting Timestamp to LocalDateTime else where.

Hopefully @Convert will be implemented by objectdb soon. 

 

#11

After now three years, why does ObjectDB still not support new Java 8 date and time API and also not the JPA converter?

#12

Java 8 Data and Time are included in JPA 2.2, which has not be released yet.

JPA converters (added in JPA 2.1) will be implemented, although the current demand for it by our clients seems to be relatively low, and it was never in a very high priority.

ObjectDB Support
#13

LocalDateTime (and other Java 8 date/time types) are now supported by ObjectDB version 2.8.3 (with some limitations). More details on this thread.

ObjectDB Support

Reply