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

  • Home
  • SEARCH
  • 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 7641779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:00:43+00:00 2026-05-31T09:00:43+00:00

If I query in SQL: select * from Profesor inner join Estudiantes on Profesor.id

  • 0

If I query in SQL:

select * from Profesor
inner join Estudiantes on Profesor.id = Estudiante.id
where Profesor.nombre = 'juan'
  and Estudiante.nombre = 'jose'

This query return the profesor and the student. One profesor and one student. Just Profesor Juan with Jose as Student.

Then if I Query in JPA:

select p from Profesor p
inner join p.estudiantes e
where p.nombre = juan
  and e.nombre = jose.

JPA will return profesor Juan with all the students not just the one I wanted and profesor.estudiantes will have the list with all the students.

My types are:

class Profesor{
    private List<Estudiante> estudiantes;
}

class Estudiante{
    String matricula;
}

Sorry I code in spanish. I just figure out this.

I dont know if I were clear on my question, please tell me.

  • 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-31T09:00:44+00:00Added an answer on May 31, 2026 at 9:00 am

    You need to understand two things.

    First: when you say Select p from Profesor, JPA only selects the columns from the Profesor table, and returns a Profesor instance containing a collection of students that is not loaded yet. It’s loaded lazily the first time you actually access the collection. And when it loads the collection, it has forgotten which query you used to load the professor. What it loads is the collection of students of the professor. And since a professor has many students, it ;oads them all. The initial query is similar to

    select p.* from Profesor inner join ...
    

    Execute it in SQL, and you’ll see that it doesn’t load a professor and its student. It only loads a professor.

    Second: an entity is supposed to represent the data in the database. It isn’t supposed to represent the result of a query. So, the collection of students in a Professor entity is always the collection of all the students of the professor.

    To do what you want, you have several choices:

    1. select p, s from Profesor inner join p.students s...: this will return an array containing the found profesor, and the found student.
    2. if the association is bidirectional: select s from Profesor p inner join p.students s ...: this will load the student, and it references its professor
    3. if using Hibernate, which violates the JPA spec in this area: select p from Profesor inner join fetch p.students...: the fetch makes hibernate load the professor and its students in a single query. But since you added a where clause on the student, it only loads the matching students of the professor.

    Note that the third solution is a very dangerous one, and I wouldn’t advise using it. First because it’s not valid JPQL. And more importantly, because the code getting a Professor entity loaded by such a query expects that professor.getStudents() will return all the students of the professor, and not just one of them. It could thus display false results, or modify the collection and cause incoherences in the database.

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

Sidebar

Related Questions

i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
SQL query: SELECT ArticleCategories.Title AS Category, Articles.Title, Articles.[Content], Articles.Date FROM ArticleCategories INNER JOIN Articles
Let's say I have the following SQL query: SELECT * FROM employee INNER JOIN
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
When using this SQL query: Select * from table_name what happens if the name
If I run the following SQL query SELECT * FROM A LEFT JOIN B
I seem to have a problem with this SQL query: SELECT * FROM appts
I written a query in my sql like this: select * from table_name order
if i apply a sql query statement to this table select * from context.tablename,
Let's say I have this sql query: SELECT * FROM questions ORDER BY votes

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.