You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Henno Vermeulen <he...@huizemolenaar.nl> on 2011/05/03 11:55:23 UTC

keeping construction order by using a Date or Timestamp field

Is there a way to set a date/time field to the current time so that selecting entities ordered by this field will retain the real construction (not necessarily insertion) order?

So ideally it would be a function that creates Timestamps and is guaranteed to return a larger Timestamp if you call it right after creating a Timestamp. But I can't find such a TimeStamp constructor. I know something like that must exist internally for version columns in OpenJPA, but my AbstractEntity class already has a version field of type Long which I don't really want to change.

What I tried was this:
            @Temporal(TemporalType.TIMESTAMP)
            @Column(nullable = false)
            private Date creationTime;

When I do creationTime = new Date() in my constructor I get this when creating and persisting 10 entities:

2011-05-03 11:22:53.8700000
2011-05-03 11:22:53.8700000
2011-05-03 11:22:53.8800000
2011-05-03 11:22:53.8800000
2011-05-03 11:22:53.8800000
2011-05-03 11:22:53.8900000
2011-05-03 11:22:53.8900000
2011-05-03 11:22:53.9000000
2011-05-03 11:22:53.9000000
2011-05-03 11:22:53.9100000

This is not really accurate enough to guarantee the order.

Regards,
Henno Vermeulen
Huize Molenaar


RE: keeping construction order by using a Date or Timestamp field

Posted by Henno Vermeulen <he...@huizemolenaar.nl>.
Well I'm not sure if I'm entirely awake but I can of course just use the auto generated id column for the order.
I am interested in an answer to my question about high precision timestamps but it is not very important.

Henno

-----Oorspronkelijk bericht-----
Van: Henno Vermeulen [mailto:henno@huizemolenaar.nl] 
Verzonden: dinsdag 3 mei 2011 12:00
Aan: 'users@openjpa.apache.org'
Onderwerp: RE: keeping construction order by using a Date or Timestamp field

Oops... a version column will never work in this case anyway because it is used for single entities, not over all entities.

I assume OpenJPA can internally create a Timestamp using System.nanoTime(). Is there a way to reuse this code to create my own Timestamps with the precision I want?

Henno

-----Oorspronkelijk bericht-----
Van: Henno Vermeulen [mailto:henno@huizemolenaar.nl] 
Verzonden: dinsdag 3 mei 2011 11:55
Aan: 'users@openjpa.apache.org'
Onderwerp: keeping construction order by using a Date or Timestamp field

Is there a way to set a date/time field to the current time so that selecting entities ordered by this field will retain the real construction (not necessarily insertion) order?

So ideally it would be a function that creates Timestamps and is guaranteed to return a larger Timestamp if you call it right after creating a Timestamp. But I can't find such a TimeStamp constructor. I know something like that must exist internally for version columns in OpenJPA, but my AbstractEntity class already has a version field of type Long which I don't really want to change.

What I tried was this:
            @Temporal(TemporalType.TIMESTAMP)
            @Column(nullable = false)
            private Date creationTime;

When I do creationTime = new Date() in my constructor I get this when creating and persisting 10 entities:

2011-05-03 11:22:53.8700000
2011-05-03 11:22:53.8700000
2011-05-03 11:22:53.8800000
2011-05-03 11:22:53.8800000
2011-05-03 11:22:53.8800000
2011-05-03 11:22:53.8900000
2011-05-03 11:22:53.8900000
2011-05-03 11:22:53.9000000
2011-05-03 11:22:53.9000000
2011-05-03 11:22:53.9100000

This is not really accurate enough to guarantee the order.

Regards,
Henno Vermeulen
Huize Molenaar



RE: keeping construction order by using a Date or Timestamp field

Posted by Henno Vermeulen <he...@huizemolenaar.nl>.
Oops... a version column will never work in this case anyway because it is used for single entities, not over all entities.

I assume OpenJPA can internally create a Timestamp using System.nanoTime(). Is there a way to reuse this code to create my own Timestamps with the precision I want?

Henno

-----Oorspronkelijk bericht-----
Van: Henno Vermeulen [mailto:henno@huizemolenaar.nl] 
Verzonden: dinsdag 3 mei 2011 11:55
Aan: 'users@openjpa.apache.org'
Onderwerp: keeping construction order by using a Date or Timestamp field

Is there a way to set a date/time field to the current time so that selecting entities ordered by this field will retain the real construction (not necessarily insertion) order?

So ideally it would be a function that creates Timestamps and is guaranteed to return a larger Timestamp if you call it right after creating a Timestamp. But I can't find such a TimeStamp constructor. I know something like that must exist internally for version columns in OpenJPA, but my AbstractEntity class already has a version field of type Long which I don't really want to change.

What I tried was this:
            @Temporal(TemporalType.TIMESTAMP)
            @Column(nullable = false)
            private Date creationTime;

When I do creationTime = new Date() in my constructor I get this when creating and persisting 10 entities:

2011-05-03 11:22:53.8700000
2011-05-03 11:22:53.8700000
2011-05-03 11:22:53.8800000
2011-05-03 11:22:53.8800000
2011-05-03 11:22:53.8800000
2011-05-03 11:22:53.8900000
2011-05-03 11:22:53.8900000
2011-05-03 11:22:53.9000000
2011-05-03 11:22:53.9000000
2011-05-03 11:22:53.9100000

This is not really accurate enough to guarantee the order.

Regards,
Henno Vermeulen
Huize Molenaar