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 8903953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:59:56+00:00 2026-06-15T01:59:56+00:00

I have been struggling with this for a while, so I am hoping that

  • 0

I have been struggling with this for a while, so I am hoping that someone may be able to give me some guidance. I have two tables:

WardTransactions:
    +-----------+
    | Field     |
    +-----------+
    | Id        |
    | VisitId   |
    | Event     |
    | EventTime |
    +-----------+

Visits:
    +---------------+
    | Field         |
    +---------------+
    | Id            |
    | VisitNumber   |
    +---------------+

There can be many WardTransactions for each visit. I am trying to use Hibernate Criteria/Projections to find the most recent WardTransaction for a specific visit. For example:

public WardTransaction findLast(Visit visit) {
    DetachedCriteria maxQuery = DetachedCriteria.forClass( WardTransaction.class );
    maxQuery.setProjection( Projections.max( "id" ) );
    Criteria query = getSession().createCriteria( WardTransaction.class )
        .createAlias("visit", "visit")
        .add(Restrictions.eq("visit.id", visit.getId()))
        .add(Property.forName( "id" ).eq( maxQuery ));
    return (WardTransaction) query.uniqueResult();
}

This is not working. From looking at the resulting query, it appears that it is doing an inner join with the Max WardTransaction.Id rather than the Maximum for the records with the required Visit. That is (modified to make it clearer):

select
        a.Id,
        a.VisitId,
        a.Event,
        a.EventTime,
        b.Id,
        b.VisitNumber,
    from
        WardTransactions a 
    inner join
        Visits b 
            on a.VisitId=b.Id 
    where
        b.Id='22' 
        and a.Id = (
            select
                max(a.Id) as y0_ 
            from
                WardTransactions a
        )

I have tried other approaches, but none result in the solution I am looking for.
Any advice, guidance appreciated.

  • 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-06-15T01:59:57+00:00Added an answer on June 15, 2026 at 1:59 am
    String hql = "select w from WardTransaction w where w.visit = :visit order by w.id desc";
    WardTransaction last = session.createQuery(hql)
                                  .setParameter("visit", visit);
                                  .setMaxResults(1)
                                  .uniqueResult();
    

    You can of course do the same with Criteria, but HQL is much more readable, IMHO.

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

Sidebar

Related Questions

I've been struggling with this for a while. Basically, I want to have two
I have been struggling with this for a while, so I hope some of
I have been struggling with this for a while and could really use some
Hi all i have been struggling this for a while now! I have some
I have been struggling with this all morning and was hoping to get some
I have been struggling with this for a while now. given a set of
I've been struggling with this issue for a while now. I have OpenCart 1.5.2.1
I have been struggling with this error for more than two hours: error: aggregate
I have been struggling with this for quite some time having been accustomed to
I have been struggling with this piece of code for a while and I

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.