I am making a request:
String query = "SELECT DISTINCT a FROM A a FETCH ALL PROPERTIES " +
"JOIN a.Bs AS b " +
"JOIN b.Cs AS c WHERE c = :c";
Query q = DAO.getSession().createQuery(query);
q.setParameter("c", c);
return q.list();
Despite what I said FETCH ALL PROPERTIESin a, when I access all the collections that are A, they still need to be loaded, so they are not loaded. They were defined as lazy loading, and this is the default behavior that I want, but this is the exception: I would like them to load right now. I tried replacing JOINwith LEFT OUTER JOINto provoke Hibernate to load them, and I tried installing q.setFetchMode("a", FetchMode.EAGER), but for Query it does not exist.
The As list is quite long, and they have quite a few collections, so make this request n + 1 very slow (about ten seconds, in contrast to one request, which will be sub-second speed). I would prefer a single request and download everything that was needed this time. Any suggestions on how I can do this?
PS, Little bonus question: if I replace the string "JOIN b.Cs AS c WHERE c = :c";
with "WHERE :c IN b.Cs";, I get an SQL exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1
The double parathesis to which he refers is equal to "and (" 1510000000000 "in (.))", Where 1510000000000 is the primary key c. Any idea why I get this error when I do it this way compared to not getting it when I do it with b.Cs joining in?
UPDATE, , . B C :
@Entity
@Table(name = "tblA")
public class A {
@Id
String AId;
@Column(name = "shortName", length = 12, nullable = false)
String shortName;
@OneToMany(fetch=FetchType.LAZY, mappedBy="theA")
private Set<B> Bs;
@OneToMany(fetch=FetchType.LAZY, mappedBy="theA")
private Set<D> Ds;
@OneToMany(fetch=FetchType.LAZY, mappedBy="theA")
private Set<E> Es;
@OneToMany(fetch=FetchType.LAZY, mappedBy="theA")
private Set<F> Fs;
}
theA B, D, E F :
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "AId", nullable = true)
@ForeignKey(name="FK_KategoriID")
private A theA;