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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:00:23+00:00 2026-05-28T01:00:23+00:00

I have two tables as below: Student: oneID (primary key), col1, col2, col3, subId,

  • 0

I have two tables as below:

Student: oneID (primary key), col1, col2, col3, subId, col4
Subject: subID (primary key), col1, col2

Every student will have exactly one or none of the subjects assigned.

So in the case: one subject assigned: subId in Student will have some value which maps to subID in Subject table

and in the case: 0 subject assigned: subId in Student will be null.

With above scenario, I have a left join query as below:

select st.col1, st.col2, su.col1, su.col2 from Student st left join Subject su on st.subId = su.subId where st.oneId = 'abc'

How to write the same exact query in Hibernate ?

select st.col1, st.col2, su.col1, su.col2 from Student st left join st.sub as su where st.oneId = 'abc'

Above query does not work and gives the below error:

"org.hibernate.hql.ast.QuerySyntaxException: Path expected for join!"

What am I missing ?

In my java code I have kept both the tables independent. That is there is no relationship between the tables defined.

Persistence.xml is as below:

<persistence-unit name="myEntityManager" transaction-type="RESOURCE_LOCAL">              
  <provider>org.hibernate.ejb.HibernatePersistence</provider>    
  <class>com.myPackage.Student</class>       
  <class>com.myPackage.Subject</class>   
</persistence-unit> 

POJOs are as below:

Student

@Entity
@Table(name = "STUDENT")
public class Student implements Serializable {

@Id
@Column(name = "ONEID")
private String oneId;

private Subject sub;

//other columns here

public void setSub(final Subject sub) {
    this.sub = sub;
}

@OneToOne(cascade = CascadeType.ALL)
public Subject getSub() {
    return this.sub;
}
} 

Subject

@Entity
@Table(name = "SUBJECT")
public class Subject implements Serializable {

@Id
@Column(name = "SUBID")
private String subId;

//other columns here
} 
  • 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-28T01:00:24+00:00Added an answer on May 28, 2026 at 1:00 am

    As mentioned in this hibernate forum thread, it’s currently not possible in Hibernate to invoke left join on disassociated entities. There’s also an opened Hibernate issue for it.

    What you should do instead, is define a one-to-one or one-to-many relationship between Student and Subject (one-to-one example, many-to-one example).

    Then, try this HQL, assuming the reference in Student to its subjects is named subjects:

    select st.col1, st.col2, su.col1, su.col2 from Student st left join st.subjects su where st.oneId = 'abc'

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

Sidebar

Related Questions

i have two tables both are related with primary-foreign key ralation and i have
I have two tables, the first has a primary key that is an identity,
I have two tables as defined below: EMAILS ID SUBJECT BODY 1 Test1 blah
I have two tables, Subject and Content, where Content references Subject with foreign key.
I have two tables: foo (primary key: foo_id) and foo_entry (primary key: foo_entry_id; foreign
I have two tables like the ones below. I need to find what exchangeRate
I have two tables names book_list and book_category . Schema is like below: book_list
I have 10 tables in my database(MySQL). two of them is given below tbl_state
I have two tables like the below one in my DB. In the first
so I have two tables. They are pictured below. I have a master table

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.