-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate query: need help
PostPosted: Thu Mar 31, 2011 7:24 pm 
Newbie

Joined: Thu Jan 06, 2011 7:05 pm
Posts: 3
Hi,
I'm trying to create correct query but unfortunately without success.
Let's say I want to show some sport events grouped by season, country and tournament type (league) (for example footbal championship):
I have some entities according to my task:
Code:
@Entity
@Table(name = "seasons")
public class Season{
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;
   
    @Column (name="year")
     private Integer year;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "season", cascade = CascadeType.ALL)
    private List<Country> countries = new ArrayList<Country>(0);

//getters and setters
   
}

Country.java
Code:
@Entity
@Table(name = "countries")
public class Country {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;

    @Column (name="name")
     private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @ForeignKey(name = "season_fk")
    @JoinColumn(name = "season_fk", nullable = false)
    private Season season;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "country", cascade = CascadeType.ALL)
    private List<League> leagues = new ArrayList<League>(0);

}

and League.java:
Code:
@Entity
@Table(name = "leagues")
public class League{
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;
   
    @Column(name = "name")
    private String name;
   
    @ManyToOne(fetch = FetchType.LAZY)
    @ForeignKey(name = "country_fk")
    @JoinColumn(name = "country_fk", nullable = false)
    private Country country;
//getters and setters
}


Now I want to build something like a report where all leagues will be grouped by countries and years and filtered by some property, like League.name.
So my query looks like:
Code:
select distinct s from League l
join l.country c
join c.season s
where l.name like :legueName


But when I start iterating the result list I get all countries and leagues without checking for where condition. I think that is due to lazy loading of associated collections. I need lazy loading, and want just for this particular query perform eager fetch and get properly filtered result. Could somebody give me a hint about how to achieve what I want.
Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Hibernate query: need help
PostPosted: Fri Apr 01, 2011 10:34 am 
Newbie

Joined: Thu Jan 06, 2011 7:05 pm
Posts: 3
Can't believe that there is no solution for this task.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.