Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HQL query analog for the SQL expression

There are two entities. Meeting and Topic (corresponding tables are Meetings and Topics)

Where one meeting may contain a set of topics.

Example table structure is as follows:

tables

In my java code Meeting class has the following declaration:

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "MeetingTopics",
joinColumns = {
    @JoinColumn(name = "meetingId")},
inverseJoinColumns = {
    @JoinColumn(name = "topicId")})    
@Expose
private Set<Topic> topicsList;

I want to get a list of meetings (with topics inside of them), where someTopicField of a topic is equal to a particular value, let say it is 11

SQL expression to get what I want looks like that:

SELECT meetings.meetingcomment, 
       topics.topicsubject, 
       topics.sometopicfield 
FROM   topics 
       INNER JOIN (meetings 
                   INNER JOIN meetingtopics 
                           ON meetings.meetingid = meetingtopics.meetingid) 
               ON topics.topicid = meetingtopics.topicid 
WHERE  ( topics.sometopicfield  = 11 ); 

But I would like to use HQL expression to solve this task, and I did not figure it out, how could I do that.

I've read this documentation but did not understand, how to apply those examples to my question.

What HQL expression will do the same as above described SQL does?

Thank you.


2 Answers

Try something like this:

select m.meetingcomment,
       t.topicsubject,
       t.sometopicfield
from Meeting as m
inner join m.topicsList as t
with t.sometopicfield = 11

If you need only the Meeting class fields the hql is:

select m from Meeting as m
inner join m.topicsList as t
with t.sometopicfield = 11

And you get a list of Meeting objects.

like image 115
dcernahoschi Avatar answered May 18 '26 00:05

dcernahoschi


Something like this should work

select meeting.meetingcomment,
       topic.topicsubject,
       topic.sometopicfield
from Meeting meeting
inner join meeetings.topicsList as topic
where topic.sometopicfield = 11
like image 20
Timo Westkämper Avatar answered May 17 '26 22:05

Timo Westkämper