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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:10:38+00:00 2026-06-09T11:10:38+00:00

I have two tables – one containing Address and another containing Photographs. The only

  • 0

I have two tables – one containing Address and another containing Photographs. The only common field between them is the PersonID. These were mapped to two POJO Classes Address and Photo. I was able to fetch details in these tables by creating criteria and adding restrictions on the fields . How should we write a join on the two tables. Is it possible to get the result as two objects -Address and Photo.

I want to do a left join so that i can get records of persons without photos as well.
I have read that this is possible only using hql but Can this be done using criterias as well?

  • 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-09T11:10:40+00:00Added an answer on June 9, 2026 at 11:10 am

    You can easily write HQL query that will return result as two objects using Theta Join (as Adrian noted). Here is an example:

    String queryText = "select address, photo from Address address, Photo photo " 
                     + " where address.personID=photo.personId";
    List<Object[]> rows = session.createQuery(queryText).list();
    
    for (Object[] row: rows) {
        System.out.println(" ------- ");
        System.out.println("Address object: " + row[0]);
        System.out.println("Photo object: " + row[1]);
    }
    

    As you can see query returns list of Object[] arrays that represents each fetched row. First element of this array will contain one obejct and second element – another.

    EDIT:

    In case of left join I think you need to use native SQL query (not HQL query). Here how you can do this:

    String queryText = "select address.*, photo.* from ADDRESS address 
                        left join PHOTO photo on (address.person_id=photo.person_id)";
    
    List<Object[]> rows = sess.createSQLQuery(queryText)
                              .addEntity("address", Address.class)
                              .addEntity("photo", Photo.class)
                              .list();
    

    This should work for your case.

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

Sidebar

Related Questions

I have two tables. One table contains information about Assets, another Table about their
I have two tables. One is temp table and another is main table. In
Have two tables with a linking table between them. USERS +-------+---------+ | userID| Username|
I have two tables, lets say table1 and table2 with common columns, id and
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables in my MySQL database, one is a library of all
I have two tables connected with one to many relationship. Parent Table is a
I have two tables inside a database. One stores unique userNames and a unique
I have two tables with this structure: Table one: ID Description Table two: ID
I have two tables: one table (okay, it's a view), vComputer, lists many computers

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.