0
@Entity
public class Device {
    private long id;
    private String deviceName; //column D_NAME
    private Set<LoginDate> loginDates;
}

/**
    Audit table being filled by some other processes on the network
 */

@Entity
public class LoginDate {
    private long id; //pk
    private String deviceName; //column D_NAME
    private Date loginDate;
}

My aim to display the last login date of the device on the report screens; I have implemented OneToMany relation between Device and LoginDate tables where it is using OrderBy to order loginDates descending so that I can choose the first item to display on the screen. But that's a bit expensive operation when it comes to the number of logins becomes larger and larger. What should I do to achieve to select only the last login date;

  • I can use @Formula of Hibernate to generate a Pojo via it's constructor with related parameters. But that will trigger number of selects per item in the report screen.

  • I can implement another method on the business layer so that once the list loaded; I can initiate another HQL with related D_NAMES to fetch the D_NAME, loginDate couples and loop through them. Which is one single select from the DB but a mapping work on the application side.

So do you have some other things for me to suggest something like a query with joins to select Device and it's last loginDate (not dates) at once ? I think This should somehow require where clause on the join site but then I hit the wall and could not figured out how to do that.

What is your suggestion ?

BTW I did not post all the properties of the Device but as you might realize that a lot of properties there and most of the other properties are getting fetched by a select with joins to other related tables/entites using HQL and LEFT OUTER JOINS.

2 Answers 2

1

I normally create a database view say, device_summary_data and then map that to Device using the @SecondaryTable annotation (or as another Entity using @OneToOne).

https://docs.oracle.com/javaee/7/api/javax/persistence/SecondaryTable.html

View:

create view device_summary_data as 
select d.id as device_id, max(l.login_date), count(l.device_name)
from logins l
inner join devices d on d.D_NAME = l.D_NAME
group by d.id

Entity:

@Entity
@SecondaryTable(name = "device_summary_data",  
   pkJoinColumns=@PrimaryKeyJoinColumn(name="device_id", referencedColumnName = "id"))
public class Device {
    private long id;

    private String deviceName; 

    private Set<LoginDate> loginDates;

    @Column(table="device_summary_data", insertable=false, updateable = false)
    private Date lastLogin;

    @Column(table="device_summary_data", insertable=false, updateable = false)
    private Integer numberOfLogins;
}

Advantages over Hibernate's @Formula:

  • works with any JPA implementation
  • filtering, sorting and paging can be applied at the DB level as per any other property.
7
  • Thanks for the idea. I will give it try. I actually can check documents but since mentioned here and for others refering to here too; Does @Formula works on app level or does it applied to app level or db level based on the formula's form ?
    – Olgun Kaya
    Nov 6, 2018 at 19:53
  • what If I want to stick with Hibernate; do you think getting the view into my app as an Entity and include in my join cases would solve my issue ?
    – Olgun Kaya
    Nov 6, 2018 at 20:02
  • 1
    You have various options and only you can decide what you want to do.
    – Alan Hay
    Nov 6, 2018 at 20:18
  • I have gone through this way using another Entity and OneToOne relation. But for those non existing rows (means there is no login yet) hibernate can not set anything to the relation object (LoginDate) and thus while trying to reach the properties of device.loginDate.timeStamp on jsf. Exception (ObjetNotFoundException) thrown and page failed to be rendered. I don't know what I am doing wrong and I don't want to manipulate setter method with try catch. Why does hibernate not injecting a null object for such cases ? Or again me missing something ?
    – Olgun Kaya
    Nov 7, 2018 at 13:32
  • If there's no record then you#ll need to add a check in your view for null.
    – Alan Hay
    Nov 7, 2018 at 14:49
1

I would do it by inverting the mapping and making some JPQL pageable / HQL query.

So your LoginDate would be something like:

@Entity
public class LoginDate {
    private long id; //pk
    @ManyToOne
    private Device device;
    private Date loginDate;
}

and custom query (JPQL) would be something like

SELECT ld FROM LoginDate ld WHERE device=:device ORDER BY ld.loginDate DESC

Depending on your other libraries / frameworks you would need to make the JPQL query to limit into one result only unless you are able to use simple native query like:

SELECT ld.logindate
FROM logindate ld
WHERE ld.device_id=:device_id
ORDER BY ld.logindate DESC
LIMIT 1

You might still leave Set<LoginDate> to your Device but add fetch=FetchType.LAZY to it for other purposes.

3
  • Thanks, If I understand you correct, you are saying to crete a bidirection with lazy loading (1-n on Device and n-1 on LoginDate). But depending on my performance case; this may not give what I expect. Cuz when it comes to the report creation I will have to wait one by one lazy load calls from the DB. I am actually trying to find a way of join structure so that one select can select all these per row from DB. Answer from @alan-hay may light my way.
    – Olgun Kaya
    Nov 6, 2018 at 20:00
  • 1
    @OlgunKaya Not quite. I just mentioned that it CAN be bidirectional with lazy but you search the latest row from LoginDate per Device. And if you need to fetch all the Devices with latest LoginDates it is just a matter of grouping which makes things even easier if using JPQL. And the Device in LoginDate is not lazy loaded.
    – pirho
    Nov 6, 2018 at 20:25
  • that's the issue. since the table is being filled by some other process that I do not manage at some point some devices on my site may not be exist on the login table site and it is not a must to show the login date. Some rows may in the report may have empty login. And since above jpql will return base on login to correlate/calculate final list using my device table and login table. right ?
    – Olgun Kaya
    Nov 7, 2018 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.