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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:33:08+00:00 2026-06-01T05:33:08+00:00

I have a OneToMany relationship: public class Class1{ private String attr1; private String attr2;

  • 0

I have a OneToMany relationship:

public class Class1{

    private String attr1;
    private String attr2;

    @OneToOne
    private Class2 object1;

}

public class Class2{
    private String attr3;    
}

The relationship can store null objects (I mean that an instance of Class1 can have null in object1). I want to do a complete search in all attributes so I’ve created this HQL query:

SELECT p FROM Class1 p
WHERE 
(
upper(p.attr1) LIKE :filter OR
upper(p.attr2) LIKE :filter OR
upper(p.object1.attr3) LIKE :filter
)

and I pass a String param in uppercase with “%” before and after:

query.setParameter("filter", "%"+filter.trim().toUpperCase()+"%");

It works OK but for the entities which have object1 = null. It never get those entities. Indeed if I remove the last condition (upper(p.object1.attr3) LIKE :filter) it works but I need to include it.

I’ve though I should do a LEFT JOIN with that relationship so I’ve tried to write it as :

SELECT p FROM Class1 p LEFT JOIN p.object1 AS p1
WHERE 
(
upper(p.attr1) LIKE :filter OR
upper(p.attr2) LIKE :filter OR
upper(p1.attr3) LIKE :filter
)

or like

SELECT p FROM Class1 p LEFT JOIN p.object1 AS p1
WHERE 
(
upper(p.attr1) LIKE :filter OR
upper(p.attr2) LIKE :filter OR
(p1 is not null AND upper(p1.attr3) LIKE :filter)
)

but without any success.

Any idea?

Thanks.

  • 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-01T05:33:10+00:00Added an answer on June 1, 2026 at 5:33 am

    As I understand you need the values either p1.attr3 is null and when it is not null it should validate the condition. So, I am just fine tuning your last query;

    SELECT p FROM Class1 p LEFT JOIN p.object1 AS p1
    WHERE 
    (
    upper(p.attr1) LIKE :filter OR
    upper(p.attr2) LIKE :filter OR
    (p1.attr3 is null OR upper(p1.attr3) LIKE :filter)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If i have a @OneToMany relationship with @Cascade(CascadeType.SAVE_UPDATE) as follows public class One {
I have bi-directional relationship like this... Person.java public class Person{ @JsonIgnore @OneToMany(targetEntity=PersonOrganization.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER,
I have two entites, Person and Teacher , with a OneToMany relationship: public class
I have a one way @OneToMany relationship between a Team and Player class. I
I have Car and Rental models conntected OneToMany relation: @Entity public class Rental {
I have the following entities: @Entity public class Owner{ @Id @Column(name = OWNER_ID) @OneToMany()
I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements
I have an Class with a unidirectional one to many relationship as follows: public
I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =
I have a parent-child relationship: @Entity @Table(name = user) public final class User {

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.