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

  • SEARCH
  • Home
  • 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 6055683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:17:32+00:00 2026-05-23T08:17:32+00:00

I have a mapped Message object which has a parent-child relationship with itself. So

  • 0

I have a mapped Message object which has a parent-child relationship with itself. So each Message holds a Set<Message> comments. Besides the comments I would like to setup a one-to-one relationship with the last comment made so that I can use the last comment in my Criteria. In SQL I establish this like follows (no comment can have the same postDate so this does not result in double matches for me):

SELECT 
    parentM.*, 
    lastComment.* 
FROM 
    messages parentM 
    left outer join messages lastComment ON 
        lastComment.parentId=parentM.messageId AND 
        lastComment.postDate=(SELECT MAX(subM.postDate) FROM messages subM WHERE subM.parentId = parentM.messageId); 

Now I would like to know how to set this up in a Hibernate xml mapping but am unsure how to use the formula property or if I should be using a different mechanism. I hoped the following would work but it does not.

<one-to-one name="latestComment"  
    class="Message"  
    cascade="none"
    property-ref="parent"
    formula="postDate=(MAX(allComments.postDate) FROM messages allComments WHERE allComments.parentId=id)" 
    lazy="proxy">
</one-to-one>

With this mapping I get the result I want as long as there is only one comment. But the forumla is completely ignored. The examples I have seem all use simple formula’s just mapping on a different column, not using a subquery.

Looking forward to some pointers

  • 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-23T08:17:32+00:00Added an answer on May 23, 2026 at 8:17 am

    I could be totally wrong here (please excuse me if I am), but I can’t help feeling that the scenario you are trying to handle may be better served by a triggered procedure on the database. Let the database engine itself handle updating the pointer to the “most recent” comment. That way you don’t have a potentially expensive MAX function – you just look at the postDate for that comment.

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

Sidebar

Related Questions

I have an object that is mapped to have a set of objects, very
I have about 100 error marker 50 each message. But not every Entity has
I have an ORM mapped object, that I want to update. I have all
I have a Message object associated with a User object (user_from and user_to). I
We have a solution which has a number of different web applications in it
I have an entity which has a collection as one of its attributes. That
I have mapped several java classes like Customer, Assessment, Rating, ... to a database
I have mapped the database in the edmx file from the database. Now how
I have mapped a network drive to a computer in my home network. Now
I am using Windows 2003. I have mapped a web application into a virtual

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.