0

I have three classes which are country,state and suburb. Each country has many states and each state has many suburbs.

My hibernate is 4.2.1.Final.

The problem is that although FetchType of states is defined EAGER, I cant retrieve suburbs of each state.

I read this answer as well. As I do not want to retrieve all the states ad suburbs any time I am retrieving the countries. So I suppose need to override the fetching strategy using criteria rather than using
@LazyCollection(LazyCollectionOption.FALSE)

Country

@Entity
public class Country {
 private List<States> states;
 ...
 public Country(){
  this.states = new ArrayList();
 }

 @OneToMany (cascade = CascadeType.ALL)
 public List<States> getStates() {
   return states;
 }
  ....
}

States

@Entity
public class States {
  private long id;
  private String name;
  private List<Suburbs> suburbs;
  ...

  public States(){
   this.suburbs = new ArrayList();
  }

  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  public List<Suburbs> getSuburbs() {
    return suburbs;
  }
}

Suburbs

@Entity
public class Suburbs {
  private long id;
  private String name;
  ....
}

Code

Criteria criteria = session.createCriteria(Country.class, "country")
                        .createAlias("country.states", "states");
                ProjectionList pl = Projections.projectionList();
                pl.add(Projections.property("states.id").as("id"));
                pl.add(Projections.property("states.name").as("name"));
                criteria.setProjection(pl);

                criteria.setResultTransformer(new 
                             AliasToBeanResultTransformer(States.class));

                criteria.add(Restrictions.eq("country.id", id));

                List<States> statesList = new ArrayList();
                statesList = (List<States>) criteria.list();
                System.out.println(">>>" 
                            + statesList.get(0).getSuburbs().size()); 
                              >>>>> returns Zero
Community
  • 1
  • 1
Tim Norman
  • 421
  • 1
  • 12
  • 31
  • 1
    I am very new to the AliasToBeanResultTransformer but I was told i would need to create a projection for all the fields i want set in the bean, but don't take my word for it I might be terribly wrong. – Gabriel Netto Aug 08 '13 at 00:27
  • @GabrielNetto I've tried it but it ran into an exception which says "Could not find setter for suburbName on class com.project.States" – Tim Norman Aug 08 '13 at 00:40

1 Answers1

0

Inspired by this and link

User user = (User) session.createCriteria(User.class)
            .setFetchMode("permissions", FetchMode.JOIN)
            .add( Restrictions.idEq(userId) )
            .uniqueResult();
J888
  • 1,944
  • 8
  • 42
  • 76
  • 1
    When it returns the list and I am trying to access the states in runs into "... could not initialize proxy - no Session" exception – Tim Norman Aug 08 '13 at 01:31
  • 1
    Try criteria.setFetchMode("states",FetchMode.SELECT) – J888 Aug 08 '13 at 02:02
  • try to set the FetchMode on the suburbs, i did manage to fix that on the project i was working on by setting the fetchMode, but you could make that same search using State.class assuming your association goes both ways. Another thing i was told is that it always should keep the reference in both sides of the association. – Gabriel Netto Aug 08 '13 at 02:20
  • 1
    tried setFetchMode("states",FetchMode.JOIN) but runs into "could not initialize proxy - no Session" exception – Tim Norman Aug 08 '13 at 02:32