-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Many-To-Many lazy loading error
PostPosted: Sat Jan 20, 2007 1:50 pm 
Newbie

Joined: Fri Dec 22, 2006 2:32 pm
Posts: 16
Hello,

I am trying to use an @ManyToMany relationship between two of my classes, however I get the following exception:

Code:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: info.devsiden.dsj.persistancetier.data.BildeAlbumData.bilder, no session or session was closed


I have the following code in my DAO:

Code:
public BildeAlbumData getBildeAlbum(int bildealbumid) {
      Session session = getSessionFactory().getCurrentSession();
      Transaction tx = session.beginTransaction();

      BildeAlbumData bilde = (BildeAlbumData)session.load(BildeAlbumData.class, bildealbumid);

      tx.commit();
      return bilde;
   }


and this is in my BildeAlbumData:

Code:
@ManyToMany(
   targetEntity=info.devsiden.dsj.persistancetier.data.BildeData.class,
   cascade={CascadeType.PERSIST, CascadeType.MERGE}
)
@JoinTable(
   name="BildeAlbumBilde",
   joinColumns = {@JoinColumn(name="BildeAlbumID")},
   inverseJoinColumns = {@JoinColumn(name="BildeID")}
)
public Collection getBilder()
{
   return bilder;
}
public void setBilder(List bilder)
{
   this.bilder = bilder;
}


And this in my BildeData:

Code:
@ManyToMany(
        cascade={CascadeType.PERSIST, CascadeType.MERGE},
        mappedBy="bilder",
        targetEntity=BildeAlbumData.class
)
public Collection getBildeAlbum()
{
   return bildeAlbum;
}

public void setBildeAlbum(List bildeAlbum)
{
   this.bildeAlbum = bildeAlbum;
}


Both classes have this at the top of it:

Code:
@Entity()
@Proxy(lazy=false)
@Table(name="Bilde")


The code that I am attempting to use this is in a JSP, just for testing purposes at the moment:

Code:
     BildeAlbumData bad = bildeDao.getBildeAlbum(1);

     out.println("Bilde Album ID: " + bad.getBildeAlbumID() + "<br>" );
     out.println("Bilde Album Besk: " + bad.getBildeAlbumBeskrivelse() + "<br>" );
     out.println("Bilde Album Navn: " + bad.getBildeAlbumNavn() + "<br>" );
     out.println("Bilde Album Opprettet: " + bad.getBildeAlbumOpprettet() + "<br>" );
     out.println("Bilde Album SistEndret: " + bad.getBildeAlbumSistEndret() + "<br>" );
     out.println("Bilde Album Brukernavn: " + bad.getBrukernavn() + "<br>" );
     out.println("Bilde Album ErPrivat: " + bad.isBildeAlbumErPrivat() + "<br>" );
     out.println("----<br>" );

     Iterator bildeIter = bad.getBilder().iterator();
     while (bildeIter.hasNext())
     {
        BildeData bd = (BildeData) bildeIter.next();
        out.println("\tBilde ID: " + bd.getBildeID() + "<br>" );
        out.println("\tBilde Navn: " + bd.getBildeNavn() + "<br>" );
        out.println("\tBilde Beskr: " + bd.getBildeBeskrivelse() + "<br>" );
        out.println("\tBilde erPrivat: " + bd.getBildeErPrivat() + "<br>" );
        out.println("\tBilde Filanvn: " + bd.getBildeFilnavn() + "<br>" );
        out.println("\tBilde LastetOpp: " + bd.getBildeLastetOpp() + "<br>" );
        out.println("\tBilde OrginalStorrelse: " + bd.getBildeOrginalStorrelse() + "<br>" );
        out.println("\tBilde TotalStorrelse: " + bd.getBildeTotalStorrelse() + "<br>" );
        out.println("\tBrukernavn: " + bd.getBrukernavn() + "<br>" );
        out.println("----<br>" );
     }


Top
 Profile  
 
 Post subject: Solved!
PostPosted: Sat Jan 20, 2007 3:47 pm 
Newbie

Joined: Fri Dec 22, 2006 2:32 pm
Posts: 16
Ofcourse, I needed to add Eager loading to my @ManyToMany relationship:

Code:
        @ManyToMany(
        cascade={CascadeType.PERSIST, CascadeType.MERGE},
        mappedBy="bilder",
        targetEntity=BildeAlbumData.class,
      fetch = FetchType.EAGER
   )


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.