Select row with most recent date per user with 1 condition in JPA

I assume DeviceEvent.received is set in newDeviceEvent() method and it's set to current time.

In such case as there is no delay between creating deviceEvent3 and deviceEvent4, most probably they will have the same time in received field. The query will then choose the maximum of all received and according to outer condition it will select both deviceEvent3 and deviceEvent4 for which only deviceEvent3 will match as it has message equal to 1.

So I believe that because the TEST consider device3 as a Last event is actually correct (most probably unless the clocks tick between creation of the two events).


Why not create an identity column for each record to be inserted. For an instance, an ID which automatically increment upon insertion (identity). From there, it should be able to return the latest value of that ID and we base our data from the ID being returned?


You have not set - deviceEvent4.setMessage(1). Hence deviceEvent3 would be the latest device event and hence your test seems to be considering the correct device event (deviceEvent3) as the latest event

Tags:

Sql

Java

Junit

Jpa