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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:51:12+00:00 2026-05-27T03:51:12+00:00

I have three tables: User, Employee and Worker. User table has one-to-zero-or-one relationship with

  • 0

I have three tables: “User”, “Employee” and “Worker”. “User” table has one-to-zero-or-one relationship with “Worker” and the same one-to-zero-or-one with “Employee”. User entity bean has following mapping attributes:

@OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
private Worker worker;
@JoinColumn(name = "id_employee", referencedColumnName = "id")
@OneToOne
private Employee idEmployee;

My aim is to get all “User” records which have one of this attributes filled (not null). I try to use the query:

SELECT u FROM User u WHERE u.idEmployee IS NOT NULL OR u.worker IS NOT NULL
ORDER BY u.login

I suppose to get 15 records, but I get only 6. I divided this query into two separate:

SELECT u FROM User u WHERE u.idEmployee IS NOT NULL ORDER BY u.login;
SELECT u FROM User u WHERE u.worker IS NOT NULL ORDER BY u.login;

I get 9 and 6 records, respectively. Put together – required 15 records.
screenshot of queries and result sets

It looks like “OR” narrows the result set to only those records, which have worker field not null. Why does it work in such way? Thanks in advance.

  • 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-27T03:51:13+00:00Added an answer on May 27, 2026 at 3:51 am

    The worker association is mapped by a foreign key in the worker table. This means that using u.worker makes an inner join to the worker table, and the is not null is always true. The resulting SQL should look like this:

    select u.* from user u, worker w where u.id = w.user_id and w.user_id is not null.
    

    You need to use a left join to accept users having no worker:

    select u from User u
    left join u.worker w
    left join u.employee e
    where w is not null or e is not null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in HTML which has three columns i.e Error - User
I have three tables: User: UserId (pk) FirstName Lastname Messages: MessageId (pk) Heading Body
I have three tables which are defined as: class User(Base): __tablename__ = 'users' id
Consider i have a user table and I have three columns mobilePhone , homePhone
I have a table with three fields, User , City and Country , where
I have a table of users with three columns. Username Accepted Rejected User 1
I have three domain classes User , Employee and UserEmployee . class User {
Say I have a table named EMPLOYEE, and the table has a field called
I have three tables: user, friend_recommendation, and movie. I would like to get the
I have a database with three tables: user_table country_table city_table I want to write

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.