I am trying to use a FetchMode.join in a criteria as you have described. I think I may be misunderstanding what the result should be.
My simple classes are Account and User, where an Account can have many users and and user can have one account.
Account:
Code:
public class Account implements Serializable {
...
@OneToMany(mappedBy="account", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Set<User> users = new HashSet<User>();
...
}
User:
Code:
public class User implements Serializable {
...
@ManyToOne (targetEntity=org.martingilday.webapp.domain.Account.class)
@JoinColumn (name="FK_AccountID", nullable=false)
private Account account;
...
}
My query is as so
Code:
return session.createCriteria(getPersistentClass()).setFetchMode("users", FetchMode.JOIN).list();
I am trying to load all accounts and have each of their user collections constructed using just one sql statement. However when I use FetchMode.JOIN I end up with more results than anticipated. If I have two accounts each with three users then the size of my list will be six. It was my understanding that Hibernate would translate the resultset into 2 account objects with the correct users attached. Is this not the case?