-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Hibernate inner join question
PostPosted: Thu Jun 14, 2012 8:26 am 
Newbie

Joined: Thu Jun 14, 2012 7:30 am
Posts: 2
Hi,

Im new to Hibernate and have a question about how to do an inner join on a HQL query between multiple objects.

So this is my object structure - I have a Container class which contains a list of broadcasts and users:

Code:
@Entity
    @Table(name = "CONTAINER")
    @XmlRootElement
    public class Container implements Serializable {

    @OneToMany(mappedBy = "container", fetch = FetchType.EAGER)
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    @Fetch(value = FetchMode.SELECT)
    private List<ProgrammeBroadcast> broadcasts = newArrayList();

    ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "CONTAINER_USER_LINK", joinColumns = @JoinColumn(name = "CONTAINER_ID"),
        inverseJoinColumns = @JoinColumn(name = "USER_ID"))
    @Fetch(value = FetchMode.SELECT)
    private List<User> assignedUsers = newArrayList();
 


Now iv written a query that is working fine that basically returns all Containers that have an assignedUser equal to a given field:

Code:
List<Container> containers = (List<Container>) session
             .createQuery("select con from Container as con inner join con.assignedUsers au where au.id = ? ")
             .setParameter(0, searchCriteria.getUser().getId())
             .setCacheable(true)
             .list();


What I want to do is add a further restriction which basically only picks dates which are in a given time period in the ProgrammeBroadcast object (called scheduledStartTime). This is what I have written but its not working:

Code:
       List<Container> containers = (List<Container>) session
             .createQuery("select con from Container as con  inner join con.assignedUsers au where au.id = ? and con.broadcasts br where br.scheduledStartTime > ? ")
             .setParameter(0, searchCriteria.getUser().getId())
             .setParameter(1, searchCriteria.getFromDate())
             .setCacheable(true)
             .list();


It returns this error - org.hibernate.hql.ast.QuerySyntaxException: unexpected token: br near line 1, column 129 [select con from com.redbeemedia.subtitling.Container as con inner join con.assignedUsers au where au.id = ? and con.broadcasts br where br.scheduledStartTime > ? ]

If someone could let me know what I am doing wrong it would be much appreciated!

Thanks,


Top
 Profile  
 
 Post subject: Re: Hibernate inner join question
PostPosted: Thu Jun 14, 2012 9:35 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You need to rearrange parts of the query. Something like this:

select con from Container as con inner join con.assignedUsers au inner join con.broadcasts br where au.id = ? and br.scheduledStartTime > ?


Top
 Profile  
 
 Post subject: Re: Hibernate inner join question
PostPosted: Thu Jun 14, 2012 9:53 am 
Newbie

Joined: Thu Jun 14, 2012 7:30 am
Posts: 2
Hi, thanks for the response, that worked great!

Thanks,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.