Hi all,
I'm a newbie in Hibernate, sorry for a possible dull question.
I'm using in my project @FetchProfile in such way:
Code:
@FetchProfile(name = "invoice-details", fetchOverrides = {
@FetchProfile.FetchOverride(entity=Invoice.class, association="agreement", mode = FetchMode.JOIN),
@FetchProfile.FetchOverride(entity=Invoice.class, association="currency", mode= FetchMode.JOIN)
})
...
@JoinColumn(name = "inv_agr_id", referencedColumnName = "agr_id", nullable = true)
@ManyToOne(fetch = FetchType.LAZY)
private Agreement agreement;
...
@JoinColumn(name = "inv_currency", referencedColumnName = "curr_id", nullable = true)
@ManyToOne(fetch = FetchType.LAZY)
private Currency currency;
... further entity code
When I start a test:
Code:
Session sess = HibernateUtil.getSessionFactory().openSession();
sess.enableFetchProfile("invoice-details");
Transaction tx = sess.beginTransaction();
Invoice i = (Invoice) sess.get(Invoice.class, 1);
tx.commit();
sess.close();
System.out.println("ID:"+i.getInvId());
System.out.println("Agr:"+i.getAgreement().getAgrNumber());
System.out.println("Curr:"+i.getCurrency().getCurrName());
I get an exception:
org.hibernate.LazyInitializationException: could not initialize proxy - no SessionI've found that the error appears on the last line of the code.
It seems, that join fetch works only for agreements and skips currencies. When I change the order in @FetchProfile declaration, join fetch swaps too. In other words, @FetchProfile works only for the first line of the declaration.
In my opinion, both objects (agreements and currency) should be initialized.
Does anyone know where I'm wrong? Thanks.