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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:36:16+00:00 2026-05-26T22:36:16+00:00

Consider the following Entity: @Entity public class Employee { @Id @GeneratedValue private Long id;

  • 0

Consider the following Entity:

@Entity
public class Employee {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    // Assume that Project has a 'name' property
    @OneToMany(mappedBy = "manager")
    private List<Project> projects;

    @OneToOne
    private Department department;

    // Assume that Computer has a 'model' property
    @ManyToOne
    private Computer computer;

    //...
}

I want to get all Project names for given Employee.

In order to do this, I need to do a JOIN, so:

// JPQL query
SELECT p.name FROM Employee e JOIN e.projects p

// Criteria API equivalent (pseudo-code)
Root<Employee> emp = CriteriaQuery#from(Employee.class);
CriteriaQuery#select(emp.get(Employee_.projects).get(Project_.name));

And these queries are fine.

However, I cannot do:

// JPQL query
SELECT e.projects.name FROM Employee e

// Criteria API equivalent (pseudo-code)
Root<Employee> emp = CriteriaQuery#from(Employee.class);
Join<Employee, Project> empProj = emp.join(Employee_.projects);
CriteriaQuery#select(empProj.get(Project_.name));

As the JPA 2.0 specification forbids to use non-singular identification variable.

However, for singular attributes, I can access them using either JOIN or simply navigating to them using identification variable, so all of the following queries are valid and return the same result:

SELECT e.computer.model FROM Employee e
SELECT c.model FROM Employee e JOIN e.computer c

// Criteria API equivalents of the above JPQL (pseudo-code)
Root<Employee> emp = CriteriaQuery#from(Employee.class);
Join<Employee, Computer> empComp = emp.join(Employee_.computer);
CriteriaQuery#select(empComp.get(Computer_.model));

Root<Employee> emp = CriteriaQuery#from(Employee.class);
CriteriaQuery#select(emp.get(Employee_.computer).get(Computer_.model));

My questions are:
– when should I use explicit JOIN (either in JPQL or Criteria API’s join(-) method)?
– what are the advantages / disadvantages of both approaches?
– is one of those considered more efficient than the other?
– if it’s just a matter of style – which one would you prefer and why?

  • 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-26T22:36:17+00:00Added an answer on May 26, 2026 at 10:36 pm
    1. I just think about the type of the attribute. e.computer is a Computer, and I can thus call getModel() on it, so e.computer.model is OK. e.projects is a List, and I can’t call getName() on a List, so e.projects.name is not OK. You need a join each time you need to access a member of a collection.

    2. When using e.computer.id, a SQL join is not necessary (and not generated, at least by Hibernate), because the ID of the computer is in the employee table, as a foreign key. Using it is thus more efficient than using an explicit join. e.computer.model generates a SQL join, and it’s just a matter of style and preference.

    3. See 2.

    4. I usually prefer explicit joins, because… it makes them explicit. It’s also easier to transform them into left joins if needed.

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

Sidebar

Related Questions

Please consider following sample @Entity public class Abc { @Id private Long id; @Unindexed
Consider the following code: @Entity @Table(name = a) public class A implements Serializable {
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
consider the following class and struct public class Entity { public IdType Id {get;set;}
Consider the following class hierarchy: public abstract class Entity { public virtual int Id
Consider the following simple example: [DataContract({0}Base)] public class Base<T> where T : Entity<T> {
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following entity class, used with, for example, EclipseLink 2.0.2 - where the
Consider that i have two entities with following relationship: Entity A <-->> Entity B
Consider the following Entities. public class Product{ int id; Date effectiveDate; Date expiryDate; Set<Inventory>

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.