1

I'm using Hibernate 3.2.5 and JSF 2.1.

I have tables accounts, accounts_opportunities and opportunities i.e. a many-to-many relationship set up.

I've mapped all the tables i.e. models.

To save an account, I'm using the following code:

public void saveAccount(Accounts current) {


    Transaction tx = session.beginTransaction();


    session.save(current);
    session.flush();

    tx.commit();

}

I get the following error when saving the Accounts object:

failed to lazily initialize a collection of role: models.Accounts.accountsOpportunitieses, no session or session was closed

I'm new to JSF and Hibernate and cannot understand why Hibernate will not save my object!

UPDATE:

I've updated the Accounts model with the following annotation:

@ManyToMany(fetch= FetchType.EAGER)
 private Set<AccountsOpportunities> accountsOpportunitieses = new HashSet<AccountsOpportunities>(0);

Data is still not being saved to the database. I now get errors such as session closed.

Gaurav Sharma
  • 4,032
  • 14
  • 46
  • 72
  • Possible duplicate: http://stackoverflow.com/questions/5095559/spring-hibernate-many-to-many-lazyinitializationexception – Cy Pangilinan May 02 '12 at 09:27
  • Yes, it looks like a duplicate. @Gaurav: Some values in accounts or accounts_opportunities are not loaded yet, but in the moment of saving they are loaded by lazy loading. The session which originally loaded that object is already closed. Pls. read about lazy loading. – Johanna May 02 '12 at 09:33
  • I actually read that answer. I din't understand how to set lazy=false though. – Gaurav Sharma May 02 '12 at 09:34
  • I briefly read through lazy loading. I like the concept of lazy loading. I mean, I don't want to fetch all the Opportunities related to an Account unless I absolutely must. Is there no way to instruct Hibernate to only update the account I'm trying to update and forget about the related Opportunities? – Gaurav Sharma May 02 '12 at 09:49

0 Answers0