0

Job Entity

 @ManyToMany @NotFound(action = NotFoundAction.IGNORE) @JoinTable(name="JOB_ACTIVITES", joinColumns=@JoinColumn(name="jobId"), inverseJoinColumns=@JoinColumn(name="an")) private Collection<Activity> activities; 

Activity Entity

@OneToOne(fetch = FetchType.LAZY) @JoinColumns(value = { @JoinColumn(name = "dwgNo", referencedColumnName = "dwgNo"), @JoinColumn(name = "rev", referencedColumnName = "rev") }) private Drawing drawing; 

Query

public List<Activity> getActivitiesByJobId(Long jobId) { String criteria = "SELECT jb.activities FROM Job jb WHERE jb.jobId=:jobId"; List<Activity> activities = em.createQuery(criteria).setParameter("jobId", Long.valueOf(jobId)).getResultList(); return activities; } 

Error

javax.el.ELException: /job.xhtml value="#{activity.drawing.dwgNo}": org.hibernate.LazyInitializationException: could not initialize proxy - no Session

I understand the error, But I don't know how to modify my query to join the Drawing entity.

Something like

SELECT jb.activities FROM Job jb LEFT JOIN FETCH Drawing ON ACTIVITY.dwgNo=Drawing.drwNo WHERE jb.jobId=:jobId"; 

but that syntax is more like native SQL. How do you specify the path on a collection?

1 Answer 1

1

Hibernate already knowws, thanks to the mapping of the associations, how they are mapped and linked together. So it doesn't need any ON clause. You can assign an alias to every joined association in order to further join or express criterias:

select activity from Job job inner join job.activities activity left join fetch activity.drawing where job.jobId = :jobId 
Sign up to request clarification or add additional context in comments.

3 Comments

Path not found when I added "left join fetch activity.drawing". Closer inspection you have "Select Activity" whereas I am querying a collection of Activities. Activities is a collection of the Job Entity. Drawing is mapped entity of the Activity Entity. Am I missing something that you are saying?
You want all the activities, with their drawing, of a given job, if I understood correctly. That's what the above query does. activity is the alias I gave to the job.activities join. It's valid JPQL, unless your entities are not as you told in your question. Note that I don't have "Select Activity". I have "select activity". Case matters.
Yes, Ah OK, I see . About to leave work will test tomorrow. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.