java.sql.Timestamp with milliseconds

#1

Using JPA with other providers java.util.Date will not provide support to store milliseconds, this can usually be overcome using (see here http://docs.oracle.com/javase/1.5.0/docs/api/java/sql/Timestamp.html)

@Id
@Column(name = "STMP")
@Temporal(TemporalType.TIMESTAMP)
private Timestamp timestamp;

However with ObjectDB the milliseconds are not being stored, as shown this is an ID field and I am encountering duplicate ID errors trying to create multiple entries in the same second.

How can I modify my declaration to include milliseconds ?

I am not particularly attached to using java.sql.Timestamp, but I would like to use a single Date/Time field (ie, not milliseconds since the Epoch)

#2

Milliseconds are stored as demonstrated by the following example:

import java.sql.*;

import javax.persistence.*;


public class T1985 {

    public static void main(String[] args) {
        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory(
                "objectdb:$objectdb/db/test.tmp;drop");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        for (int i = 0; i < 10; i++) {
            em.persist(new MyEntity());
            try { Thread.sleep(20); } catch (InterruptedException x) {}
        }
        em.getTransaction().commit();
       
        em.close();
        emf.close();
    }
   
    @Entity
    public static class MyEntity {
        @Id Timestamp time = new Timestamp(System.currentTimeMillis());
    }
}
ObjectDB Support

Reply