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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:48:49+00:00 2026-06-13T11:48:49+00:00

I have a JPA database setup like the following: @Entity public class Contact {

  • 0

I have a JPA database setup like the following:

@Entity
public class Contact {

    @Id
    private Long id;
    @Column(length = 64)
    private String firstname;
    @Column(length = 64)
    private String surname;

    @OneToMany(mappedBy = "contact") 
    private List<Address> adresses = new ArrayList<Address>();
    @OneToMany(mappedBy = "contact") 
    private List<Telephone> telephones = new ArrayList<Telephone>();

}

@Entity
public class Address {

    @Id
    private Long id;
    @Column(length = 128)
    private String street;
    @Column(length = 16)
    private String plz;


    @ManyToOne 
    private Contact contact;

}

@Entity
public class Telephone {

    @Id
    private Long id;
    @Column(length = 32)
    private String number;


    @ManyToOne
    private Contact contact;

}

Now I have the following query for my search form:

em.createQuery("SELECT DISTINCT c FROM Contact c LEFT JOIN c.addresses a LEFT JOIN c.telephones t "
                        + "WHERE c.surnameLIKE '%"+var_surname+"%' "
                        + "AND c.firstname LIKE '%"+var_firstname+"%' "
                        + "AND a.street LIKE '%"+var_street+"%'"
                        + "AND t.nummer LIKE '%"+var_telephone+"%'"
                        ).getResultList();

My question is now how can I join all 3 tables in a single JPA-query to look up for the firstname, street and number for example? I tried already with LEFT JOIN, but I don’t get any results when for example the Telephone table is empty (it properly works if all tables have appropriate entries). I would like to have a result list with the Contacts even when there are no phone numbers and just one result even if a contact has more than one phone number for example.

Many thanks for your help 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-06-13T11:48:50+00:00Added an answer on June 13, 2026 at 11:48 am

    Thanks for your helpful answers. Finally I had to solve the problem with a dynamic buildup of my search string, selectively add/remove some parts of the join operands depending on which fields the user left empty.

        public List<Person> querySearch(String snachname, String svorname, String srechtsform, String sadresse, String sort, String sland, String stelefon, String semail, LoadGroup group) {
            if(srechtsform.equals("Alle")) {
                srechtsform = "";
            }
            if(sland.equals("Alle")) {
                sland = "";
            }
            String q = "SELECT DISTINCT * FROM Person p LEFT OUTER JOIN Adresse a ON (a.PERSON_ID=p.ID) LEFT OUTER JOIN Telefon t ON (t.PERSON_ID=p.ID) LEFT OUTER JOIN Email e ON (e.PERSON_ID=p.ID) WHERE p.nachname1 LIKE '%"+snachname+"%'AND p.vorname LIKE '%"+svorname+"%' AND p.rechtsform LIKE '%"+srechtsform+"%' ";
            if(sadresse.equals("") && sort.equals("") && sland.equals("")) {
                q += "AND (a.ID IS NULL OR (a.strasse LIKE '%"+sadresse+"%' AND a.ort LIKE '%"+sort+"%' AND a.land LIKE '%"+sland+"%')) "; 
            } else {
                q += "AND a.strasse LIKE '%"+sadresse+"%' AND a.ort LIKE '%"+sort+"%' AND a.land LIKE '%"+sland+"%' "; 
            }
            if(stelefon.equals("")) {
                q += "AND (t.ID IS NULL OR t.nummer LIKE '%"+stelefon+"%') ";
            } else {
                q += "AND t.nummer LIKE '%"+stelefon+"%' ";
            }
            if(semail.equals("")) {
                q += "AND (e.ID IS NULL OR e.mailadresse LIKE '%"+semail+"%') ";
            } else {
                q += "AND e.mailadresse LIKE '%"+semail+"%' ";
            }
            q += "GROUP BY p.ID";
            Query query = em.createNativeQuery(q, Person.class);
            if(group!=null) { query.setHint(QueryHints.LOAD_GROUP, group); }
            return query.getResultList();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JPA entity similar to; public class Inventory { private String productname;
I have an JPA entity like this: @Entity @Table(name = category) public class Category
I have a JPA entity bean similar to the following: @Entity class License {
I have a JPA object which has a many-to-many relationship like this: @Entity public
I have a JPA TableXXX class that contains the following field: @Column(name = CreatedOn)
I have a JPA entity already persisted in the database. I would like to
I have a JPA entity class with a composite primary key (uid,lid) that in
I'm using Hibernate with JPA and have a relationship that looks like this: public
A have a JPA entity that has timestamp field and is distinguished by a
I have a JPA project and I would like to insert some initial data

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.