Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use JOIN using Hibernate's session.createSQLQuery()
    primarykey
    data
    text
    <p>I have two Entity (tables) - Employee &amp; Project. An Employee can have multiple Projects. Project table's CREATOR_ID field refers to Employee table's ID field. Employee entity does not maintain any reference for Project - but Project entity has a reference for Employee.</p> <p>Using EntityManager following query works fine - </p> <pre><code>entityManager.createQuery( "select e from EmployeeDTO e, ProjectDTO p" + " where p.id = ?1 and p.creator.id=e.id"); </code></pre> <p>But since I have the LAZY association relationship, I get error:</p> <blockquote> <p>Could not initialize proxy - no Session</p> </blockquote> <p>if I try to access Project info from Employee entity. This is expected and so I am using Hibernate's Session to create query as shown below.</p> <pre><code>Session session = HibernateUtil.getSessionFactory().openSession(); org.hibernate.Query q = session.createSQLQuery( "SELECT E FROM EMPLOYEE_TAB E, PROJECT_TAB P WHERE P.ID = " + projectId + " AND P.CREATOR_ID = E.ID") .addEntity("EmployeeDTO ", EmployeeDTO.class) .addEntity("ProjectDTO", ProjectDTO.class); </code></pre> <p>But I get error like: "Column 'E' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification..."</p> <p>Can anyone suggest what will be the right JOIN syntax for such case? If I use <code>("SELECT * FROM EMPLOYEE_TAB E, ........")</code> - it gives other error: </p> <blockquote> <p>java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.im.server.dto.EmployeeDTO</p> </blockquote> <p>.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload