Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 461009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:55:53+00:00 2026-05-12T22:55:53+00:00

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements

  • 0

I have a 2 entities in a One-To-Many relationship:

OfficeView.java:

public class OfficeView implements java.io.Serializable {

    private Integer officeId;
    private String addr1;
    private String city;
    private String state;
    private String zip;
    private List<Devices> devices;

getters and setters

    @OneToMany(mappedBy = "officeView", fetch = FetchType.EAGER)
    public List<Devices> getDevices() {
        return devices;
    }

    public void setDevices(List<Devices> devices) {
        this.devices = devices;
    }

}

Devices.java:

public class Devices implements java.io.Serializable {

    private Integer devId;
    private String devName;
    private Date lastUpdate;
    private OfficeView officeView;

getters and setters

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "office_id")
    public OfficeView getOfficeView() {
        return officeView;
    }

    public void setOfficeView(OfficeView officeView) {
        this.officeView = officeView;
    }

}

For some devices, in the table there are 2 almost identical entries except for lastUpdate. I always want to retrieve the most recent entry in the table. And only that entry.

For example you have 2 entries for this device:

SSA-PITTSB-PA-NEF82 2009-10-12 23:51:07
SSA-PITTSB-PA-NEF82 2009-10-15 14:19:35

When I load the OfficeView instance for the office containing this (and other devices) I only want to fetch the most recent of these 2 devices. In the database, I can get that short list from the devices table with this SQL query:

select t.* from
  (select dev_id, dev_name, max(last_update) maxValue 
from devices
  group by dev_name) x
join devices t on x.dev_name=t.dev_name
and x.maxValue=t.last_update

SSA-PITTSB-PA-NEF82 2009-10-15 14:19:35

This is a seam application and OfficeViewHome.java, from which the query is called, extends EntityHome. I think the correct way to do this is to overload loadInstance with a customized query.

I just have no idea how to construct the query. How do I do this?

I know how to write the join query in SQL to join the oFfice_view and devices tables and get all the correct data. But I can’t use this query to load the instance (as in createNativeQuery) because the data from the devices table needs to be loaded as a list of Devices objects.

I really hope this makes sense because I am utterly stumped.

april26

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:55:53+00:00Added an answer on May 12, 2026 at 10:55 pm

    You’ll have to override collection loader – be aware that this is a Hibernate extension to JPA standard. You can use an SQL query to do this instead of writing HQL – indeed, writing HQL may be somewhat troublesome in this scenario.

    @Loader(namedQuery = "latestDevices")
    @NamedNativeQuery(name="latestDevices", query="...", resultClass = Devices.class)
    @OneToMany(mappedBy = "officeView", fetch = FetchType.EAGER)
    public List<Devices> getDevices() {
      return devices;
    }
    

    The actual query is the one you wrote above with additional condition on office_id, whose parameter value is going to be supplied by Hibernate:

    select t.* from
     (select dev_id, dev_name, max(last_update) maxValue 
        from devices
       group by dev_name) x
      join devices t on x.dev_name=t.dev_name
       and x.maxValue=t.last_update
       and t.office_id = ?
    

    If the names are not unique throughout you’ll have to rewrite the above to explicitly join office on id for the inner select because you can’t repeat the ? placeholder.

    Be very careful with updating / deleting Devices through collection on Office, Hibernate attempts to optimize certain operations and may end up deleting devices for given Office that weren’t retrieved by above select.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities, Entity1 and Entity2, on a one to many relationship. On
This is situation I have: I have two entities with one-to-many relationship mapped like
I've mapped two entities using JPA (specifically Hibernate). Those entities have a one-to-many relationship
I have two entities that usually have one-to-many relationship, but in rare cases should
Preface: I have two entities defined as a one-to-many relationship: A <<-------> B. B's
I have two entities, a User and Role with a one-to-many relationship from user
i have two entities named Parent and Child , linked in a one-to-many relationship.
I have two entities, Job and Language , in a many-to-one relationship. The mapping
I have two entities Foo and Bar with a Many to Many relationship between
I have two entities, a Shelf and a Product : public class Shelf {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.