Hi
I have a question if it is possible to somehow initialize proxy collection by Hibernate.initialize() call and have each element of the collection fetch join its one2one/many2one association?
Let's make myself clear be simple example:
Code:
@Entity
@Table(name = "customer")
public class Customer {
@Id
@Column(name = "cus_id", unique = true)
private Long customerId;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "customer")
private Set<CustomerAccount> accounts= new HashSet<CustomerAccount>();
}
Code:
@Entity
@Table(name = "customer_account")
public class CustomerAccount{
@Id
@Column(name = "cac_id")
private Long id;
@ManyToOne(optional = true)
@JoinColumn(name = "cac_cus_id", referencedColumnName = "cus_id")
private Customer customer;
@ManyToOne(optional = true)
@JoinColumn(name = "cac_ccy_symbol", referencedColumnName = "ccy_symbol")
private Currency accountCurrency;
}
Code:
@Entity
@Table(name = "currency")
public class Currency{
@Id
@Column(name = "ccy_symbol")
private String symbol;
@Column(name = "ccy_code", nullable = false)
private Long code;
@Column(name = "ccy_precision", nullable = false)
private int precision;
}
In My DAO i execute:
Code:
//find customer
Customer customerDetails = currentSession.createQuery("from Customer where ... "
//
Hibernate.initialize(customerDetails.getAccounts());
for (CustomerAccount ca : customerDetails.getAccounts()) {
Hibernate.initialize(ca.getAccountCurrency());
}
Obviuosly it leads to n+1 select problems.
Is it any way to initialize set of accounts and tell hibernate to eagerly load currency for each of the account.
From SQL pont of view the query would be trivial...