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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:43:10+00:00 2026-05-31T13:43:10+00:00

The goal is to retrieve all users (not messages) that have sent a message

  • 0

The goal is to retrieve all users (not messages) that have sent a message to a particular user (the current logged-in user), or have received a message from this user (even if they didn’t reply yet). I’ve tried many queries, but to no avail. (I have the id of the logged-in user).

Do you have any ideas? I’m assuming I need to do a join on Message & RegisteredUser classes, but not sure how it’ll work since the Message has the ‘sender’ & ‘receiver’ which makes the query a bit more complex.

RegisteredUser class (doesn’t have a reference to Message):

@Entity
@Table(name = "USER")
public class RegisteredUser implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

Message class:

@Entity
@Table(name = "MESSAGE")
public class Message implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int id;

    @ManyToOne
    @JoinColumn(name = "receiverId")
    RegisteredUser receiver;

    @ManyToOne
    @JoinColumn(name = "senderId")
    RegisteredUser sender;

I’m using jpa 2.0 and hibernate 3.5.6.

Thanks!

//EDIT — The Solution, thanks to Andrey

"select distinct u from RegisteredUser u where u in (select m1.sender from Message m1 join m1.receiver r where r.id = " + user.getId() + ") or u in (select m2.receiver from Message m2 join m2.sender s where s.id = " + user.getId() + ")"
  • 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-31T13:43:11+00:00Added an answer on May 31, 2026 at 1:43 pm
    select distinct u from RegisteredUser u where 
    u in (select m1.sender from Message m1 join m1.receiver r where r.id = " + user.getId() + ") or
    u in (select m2.receiver from Message m2 join m2.sender s where s.id = " + user.getId() + ")"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal: Retrieve all instance of List<> from interface GameFactory Problem: I retrieve error message
I am using asp.net mvc. I have generated a view that retrieve all unapproved
the goal is to retrieve the number of users in one table which have:
My goal is to present a jsf page that has Create, Retrieve and Update
My goal is to create a map of maps so that I can retrieve
goal: I have the string 1234432144 I want to only replace the first 2
Goal To use a CREATE TYPE statement in HSQLDB 2.0.0 to create a user-defined
Goal : After user saves data to my mysql DB, a JSON teaser of
i have a problem.. I use the autocomplete plugin, that i edite for make
I have two models that are associated. I have an Artist model and a

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.