title: How do I properly load my lazily associated entities (without making them eagerly loaded)?
body: <p>I'm building a WAR using Spring 3.1.1.RELEASE, Hibernate 4.1.0.Final, JPA 2.0, and replying to JBoss 7.1.1.Final.  I have this entity with a couple of lazy associations …</p>
  
  <pre><code>@GenericGenerator(name = "uuid-strategy", strategy = "uuid.hex")
  @Entity
  @Table(name = "cb_contract",
      uniqueConstraints = {@UniqueConstraint(columnNames={"OPPORTUNITY_ID","PRODUCT_ID"})}
  )
  public class Contract implements Serializable {
  
      …
      @Id
      @Column(name = "ID")
      @GeneratedValue(generator = "uuid-strategy")
      private String id;
  
      @OneToOne(fetch = FetchType.LAZY, targetEntity = Product.class)
      @JoinColumn(name = "PRODUCT_ID")
      @NotNull
      private Product product;
  
      @OneToOne(fetch = FetchType.LAZY, targetEntity = Organization.class)
      @JoinColumn(name = "ORGANIZATION_ID")
      @NotNull
      private Organization org;
  </code></pre>
  
  <p>I load these entities from one transactional method …</p>
  
  <pre><code>@Transactional
  @Service
  public class ContractServiceImpl implements ContractService
  ...
  
  public List&lt;Contract&gt; findContractByOppId(final String oppId)
  {
      final Contract contract = new Contract();
      contract.setOpportunityId(oppId);
      return m_contractDao.find(contract);
  }   // findContractByOppId
  </code></pre>
  
  <p>and then try to save them in another @Transactional method (this private method is called from the @Transactional public method):  </p>
  
  <pre><code>@Service
  @Transactional
  public class UserServiceImpl implements UserService
  {
  ...
  private void saveUserContracts(final User user, final Set&lt;Contract&gt; contracts)
  {
      if (contracts != null)
      {
          for (final Contract contract : contracts)
          {
              contract.getOrg();
              contract.getProduct();
  
              final UserContract userContract = new UserContract();
              userContract.setContract(contract);
              userContract.setUser(user);
              userContractDao.save(userContract);
          }   // for
      }   // if
  }   // saveUserContracts
  </code></pre>
  
  <p>However, upon trying to save them, I get the exception below.  How do I get the lazy entities to load (preferably using JPA as opposed to a Hibernate-specific method)?</p>
  
  <pre><code>org.hibernate.LazyInitializationException: could not initialize proxy - no Session
      at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149)
      at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
      at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
      at org.mainco.subco.organization.domain.Organization_$$_javassist_10.hashCode(Organization_$$_javassist_10.java)
      at org.mainco.subco.ecom.domain.Contract.hashCode(Contract.java:134)
      at java.util.HashMap.put(HashMap.java:372)
      at java.util.HashSet.add(HashSet.java:200)
      at java.util.AbstractCollection.addAll(AbstractCollection.java:305)
      at java.util.HashSet.&lt;init&gt;(HashSet.java:100)
      at org.mainco.subco.user.test.service.UserServiceTest.testAddUser(UserServiceTest.java:116)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  </code></pre>
  
  <p>Thanks, - Dave</p>