[hibernate-issues] [Hibernate-JIRA] Created: (HHH-5119) LazyInitializationException on OneToMany relationship if relation is not on Primary Key

Simon Martinelli (JIRA) noreply at atlassian.com
Thu Apr 15 04:33:59 EDT 2010


LazyInitializationException on OneToMany relationship if relation is not on Primary Key
---------------------------------------------------------------------------------------

                 Key: HHH-5119
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5119
             Project: Hibernate Core
          Issue Type: Bug
    Affects Versions: 3.5.0-Final
         Environment: Hibernate 3.5.0-Final with Derby DB and Oracle 10g
            Reporter: Simon Martinelli
         Attachments: JPA2_M2M.tar.gz

I created two entities One and Two. 
One has a OneToMany relationship to Two. 

@OneToMany
@JoinColumn(insertable = false, updatable = false, name = "SCHLUESSEL", referencedColumnName = "SCHLUESSEL")
private List<Two> twos = new ArrayList<Two>();

Important notice: SCHLUESSEL is not the primary key of Two!

I have the following test code:

List<One> ones = em.createQuery("select o from One o").getResultList();
for (One one : ones) {
  logger.info(one.getName());
  //em.refresh(one);
  for (Two two : one.getTwos()) {
    logger.info(two.getText());
  }
}

When I run a test and try to access "twos" I get an excpetion:

2002 [main] ERROR org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: ch.test.One.twos, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ch.test.One.twos, no session or session was closed
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)

After uncommenting em.refresh it works.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list