Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

sql

junit

jpa

I have this Entity and I want to list for each device the last event with the property message = 1

@Entity
@Table(name = "t_device_event")
@NamedQueries(value = { 
@NamedQuery(name = "DeviceEvent.findWithMessageActive",
            query = "from DeviceEvent as de1 where de1.message = 1 and de1.received = " 
                  + " ( select max(de2.received) from DeviceEvent de2 " 
                  + " where de2.device.id = de1.device.id )  "), )
public class DeviceEvent {
     ..
}

But I have an assertion problem in the last test, because it considers device3 as a last event and its not the case.

assertTrue ((numDevicesWithActiveMessage+1) == deviceEventService.findWithActiveMessage().size());

DeviceEvent deviceEvent3 = newDeviceEvent();
deviceEvent3.setMessage(1);
deviceEventService.save(deviceEvent3);

DeviceEvent deviceEvent4 = newDeviceEvent();
deviceEventService.save(deviceEvent4);

assertTrue ((numDevicesWithActiveMessage+1) == deviceEventService.findWithActiveMessage().size());
like image 729
Nunyet de Can Calçada Avatar asked Jan 25 '16 19:01

Nunyet de Can Calçada


2 Answers

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).

like image 72
Zbynek Vyskovsky - kvr000 Avatar answered Oct 21 '22 11:10

Zbynek Vyskovsky - kvr000


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?

like image 20
bipartite Avatar answered Oct 21 '22 12:10

bipartite