-->
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.  [ 7 posts ] 
Author Message
 Post subject: WrongClassException
PostPosted: Sun Mar 12, 2006 7:08 pm 
Newbie

Joined: Mon Nov 14, 2005 5:43 am
Posts: 6
Hi there,

I've read many things about WrongClassException (and of course) the solutions on the non-annotated way of using hibernate.

But I've got Hibernate running with annotations and run into trouble.

I do an Query for a specified (Just a get(WohnungMiete.java, id)) Subclass. This subclass retrieves the getTeaserImage() from the superclass on the TeaserBild-Class wich is an subclass of bilder.
But if this runs on the @OneToMany relationship to the ObjektBild
I retrieve the WrongClassException shown below.

Has anybody an solution for this? I've seen the WrongClassExceptions relating JDom but this doesn't fit to me (I think)

Thanks!

Marc

Hibernate version:
Hibernate 3.1.2
Hibernate Annotations 3.1 beta 8

Mapping documents:

Objekt.java:
Code:
package isi.as.hib.objekt;


import isi.as.hib.bilder.ObjektBild;
import isi.as.hib.bilder.TeaserBild;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
      name = "objekttyp",
      discriminatorType = DiscriminatorType.STRING
      )
public class Objekt implements Serializable {
   /**
    *
    */
   private static final long serialVersionUID = 1L;
   private String objektId;

// Properties deleted for more readability

   private List<ObjektBild> objektBilder;
   
   private TeaserBild teaserBild;


// Properties deleted for more readability

   @Id
   @GeneratedValue(generator="system-uuid")
   @GenericGenerator(name="system-uuid", strategy = "uuid")
   @Column(length=36)
   public String getObjektId() {
      return objektId;
   }

   public void setObjektId(String objektId) {
      this.objektId = objektId;
   }


// Properties deleted for more readability

   @OneToMany(targetEntity = ObjektBild.class, cascade=CascadeType.ALL )
    @JoinColumn(name="objektid")
   public List<ObjektBild> getObjektBilder() {
      return objektBilder;
   }

   public void setObjektBilder(List<ObjektBild> bilder) {
      this.objektBilder = bilder;
   }
   
   public void addObjektBild(ObjektBild bild) {
      if (this.objektBilder == null)
         this.objektBilder = new ArrayList<ObjektBild>();
      this.objektBilder.add(bild);
   }
   
   @OneToOne(cascade = CascadeType.ALL)
   @JoinColumn(name="teaserbild")
   public TeaserBild getTeaserBild() {
      return teaserBild;
   }

   public void setTeaserBild(TeaserBild teaserBild) {
      this.teaserBild = teaserBild;
   }


}


Used Sublcass of Objekt:
Code:
package isi.as.hib.objekt;

import isi.as.hib.bilder.GrundrissBild;

import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue("WOHNUNGMIETE")
public class WohnungMiete extends Objekt {

// No other relationships. All relationships are in Objekte.java

}



Bilder-Class:
Code:
package isi.as.hib.bilder;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Lob;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
      name = "bildtyp",
      discriminatorType = DiscriminatorType.STRING
      )
public class Bilder implements Serializable {

   /**
    *
    */
   private static final long serialVersionUID = 1L;
   private String bilderId;
   private byte[] bild;

   @Id
   @GeneratedValue(generator="system-uuid")
   @GenericGenerator(name="system-uuid", strategy = "uuid")
   @Column(length=36)
   public String getBilderId() {
      return bilderId;
   }
   public void setBilderId(String bilderId) {
      this.bilderId = bilderId;
   }


   @Lob
   public byte[] getBild() {
      return bild;
   }
   public void setBild(byte[] bild) {
      this.bild = bild;
   }

}


Subclass of Bilder (joined from subclass wohnungmiete)
Code:
package isi.as.hib.bilder;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue("OBJEKTBILD")
public class ObjektBild extends Bilder {

}




Code between sessionFactory.openSession() and session.close():

Code:
         if (session == null || !session.isOpen()) {
            session = HibernateUtil.getSessionFactory().openSession();
         }
         WohnungMiete wm = (WohnungMiete) session.get(WohnungMiete.class,
               getObjektId());
         setWohnungMiete(wm);
         
         ImageMetadata im = new ImageMetadata();


Full stack trace of any exception that occurs:

Code:
Caused by: org.hibernate.WrongClassException: Object with id: 402882fb09f078fc0109f07a87dd0003 was not of the specified subclass: isi.as.hib.bilder.ObjektBild (loaded object was of wrong class)
   at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1235)
   at org.hibernate.loader.Loader.getRow(Loader.java:1186)
   at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:569)
   at org.hibernate.loader.Loader.doQuery(Loader.java:689)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
   at org.hibernate.loader.Loader.loadCollection(Loader.java:1919)
   at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:71)
   at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
   at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
   at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1695)
   at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
   at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
   at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:246)
   at org.apache.tapestry.coerce.CollectionToIteratorConverter.convertValue(CollectionToIteratorConverter.java:30)
   at org.apache.tapestry.coerce.TypeConverterWrapper.convertValue(TypeConverterWrapper.java:65)
   at $TypeConverter_109f07925f4.convertValue($TypeConverter_109f07925f4.java)
   at org.apache.tapestry.coerce.ValueConverterImpl.coerceValue(ValueConverterImpl.java:107)
   at $ValueConverter_109f07924be.coerceValue($ValueConverter_109f07924be.java)
   at org.apache.tapestry.components.Foreach.getSourceData(Foreach.java:62)
   at org.apache.tapestry.components.Foreach.renderComponent(Foreach.java:83)
   at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:617)
   at org.apache.tapestry.AbstractComponent.renderBody(AbstractComponent.java:434)
   at org.apache.tapestry.form.FormSupportImpl.render(FormSupportImpl.java:486)
   at org.apache.tapestry.form.Form.renderComponent(Form.java:250)
   at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:617)
   at org.apache.tapestry.AbstractComponent.renderBody(AbstractComponent.java:434)
   at org.apache.tapestry.components.RenderBody.renderComponent(RenderBody.java:44)
   at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:617)
   at org.apache.tapestry.AbstractComponent.renderBody(AbstractComponent.java:434)
   at org.apache.tapestry.html.Body.renderComponent(Body.java:129)
   at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:617)
   at org.apache.tapestry.AbstractComponent.renderBody(AbstractComponent.java:434)
   at org.apache.tapestry.html.Shell.renderComponent(Shell.java:114)
   at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:617)
   at org.apache.tapestry.BaseComponent.renderComponent(BaseComponent.java:92)
   at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:617)
   at org.apache.tapestry.BaseComponent.renderComponent(BaseComponent.java:92)
   at org.apache.tapestry.AbstractComponent.render(AbstractComponent.java:617)
   at org.apache.tapestry.AbstractPage.renderPage(AbstractPage.java:275)
   at org.apache.tapestry.engine.RequestCycle.renderPage(RequestCycle.java:366)
   ... 37 more

Name and version of the database you are using:

Postgres 8.1.3 (win)

Debug level Hibernate log excerpt:

Code:
2006-03-12 23:24:49,312 [http-80-2] DEBUG - result set row: 0
2006-03-12 23:24:49,312 [http-80-2] DEBUG - returning '402882fb09f078fc0109f07a87dd0003' as column: bilderId36_0_
2006-03-12 23:24:49,312 [http-80-2] DEBUG - result row: EntityKey[isi.as.hib.bilder.GrundrissBild#402882fb09f078fc0109f07a87dd0003]
2006-03-12 23:24:49,312 [http-80-2] DEBUG - Initializing object from ResultSet: [isi.as.hib.bilder.GrundrissBild#402882fb09f078fc0109f07a87dd0003]
2006-03-12 23:24:49,312 [http-80-2] DEBUG - Hydrating entity: [isi.as.hib.bilder.GrundrissBild#402882fb09f078fc0109f07a87dd0003]
2006-03-12 23:24:49,312 [http-80-2] DEBUG - returning 'asd' as column: beschrei3_36_0_
2006-03-12 23:24:49,312 [http-80-2] DEBUG - returning 'false' as column: veroeffe4_36_0_
2006-03-12 23:24:49,312 [http-80-2] DEBUG - returning 'false' as column: veroeffe5_36_0_
2006-03-12 23:24:49,312 [http-80-2] DEBUG - returning 'asdf' as column: titel36_0_
2006-03-12 23:24:49,312 [http-80-2] DEBUG - returning '402882fb09f078fc0109f07a1b290001' as column: objektid1_
2006-03-12 23:24:49,312 [http-80-2] DEBUG - found row of collection: [isi.as.hib.objekt.WohnungMiete.grundrissBilder#402882fb09f078fc0109f07a1b290001]
2006-03-12 23:24:49,312 [http-80-2] DEBUG - reading row
2006-03-12 23:24:49,312 [http-80-2] DEBUG - returning '402882fb09f078fc0109f07a87dd0003' as column: bilderId1_
2006-03-12 23:24:49,312 [http-80-2] DEBUG - loading entity: [isi.as.hib.bilder.GrundrissBild#402882fb09f078fc0109f07a87dd0003]
2006-03-12 23:24:49,312 [http-80-2] DEBUG - attempting to resolve: [isi.as.hib.bilder.GrundrissBild#402882fb09f078fc0109f07a87dd0003]
2006-03-12 23:24:49,312 [http-80-2] DEBUG - resolved object in session cache: [isi.as.hib.bilder.GrundrissBild#402882fb09f078fc0109f07a87dd0003]
2006-03-12 23:24:49,312 [http-80-2] DEBUG - done processing result set (1 rows)
2006-03-12 23:24:49,312 [http-80-2] DEBUG - about to close ResultSet (open ResultSets: 1, globally: 1)
2006-03-12 23:24:49,312 [http-80-2] DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2006-03-12 23:24:49,312 [http-80-2] DEBUG - closing statement
2006-03-12 23:24:49,312 [http-80-2] DEBUG - total objects hydrated: 1
2006-03-12 23:24:49,312 [http-80-2] DEBUG - resolving associations for [isi.as.hib.bilder.GrundrissBild#402882fb09f078fc0109f07a87dd0003]
2006-03-12 23:24:49,312 [http-80-2] DEBUG - done materializing entity [isi.as.hib.bilder.GrundrissBild#402882fb09f078fc0109f07a87dd0003]
2006-03-12 23:24:49,312 [http-80-2] DEBUG - 1 collections were found in result set for role: isi.as.hib.objekt.WohnungMiete.grundrissBilder
2006-03-12 23:24:49,312 [http-80-2] DEBUG - collection fully initialized: [isi.as.hib.objekt.WohnungMiete.grundrissBilder#402882fb09f078fc0109f07a1b290001]
2006-03-12 23:24:49,312 [http-80-2] DEBUG - 1 collections initialized for role: isi.as.hib.objekt.WohnungMiete.grundrissBilder
2006-03-12 23:24:49,312 [http-80-2] DEBUG - initializing non-lazy collections
2006-03-12 23:24:49,312 [http-80-2] DEBUG - done loading collection
2006-03-12 23:24:49,312 [http-80-2] DEBUG - collection initialized
2006-03-12 23:24:49,312 [http-80-2] DEBUG - initializing collection [isi.as.hib.objekt.Objekt.objektBilder#402882fb09f078fc0109f07a1b290001]
2006-03-12 23:24:49,312 [http-80-2] DEBUG - checking second-level cache
2006-03-12 23:24:49,312 [http-80-2] DEBUG - collection not cached
2006-03-12 23:24:49,312 [http-80-2] DEBUG - loading collection: [isi.as.hib.objekt.Objekt.objektBilder#402882fb09f078fc0109f07a1b290001]
2006-03-12 23:24:49,312 [http-80-2] DEBUG - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2006-03-12 23:24:49,312 [http-80-2] DEBUG - select objektbild0_.objektid as objektid1_, objektbild0_.bilderId as bilderId1_, objektbild0_.bilderId as bilderId36_0_, objektbild0_.beschreibung as beschrei3_36_0_, objektbild0_.veroeffentlichen as veroeffe4_36_0_, objektbild0_.veroeffentlichenWeb as veroeffe5_36_0_, objektbild0_.bild as bild36_0_, objektbild0_.titel as titel36_0_ from Bilder objektbild0_ where objektbild0_.objektid=?
2006-03-12 23:24:49,312 [http-80-2] DEBUG - preparing statement
2006-03-12 23:24:49,312 [http-80-2] DEBUG - binding '402882fb09f078fc0109f07a1b290001' to parameter: 1
2006-03-12 23:24:49,328 [http-80-2] DEBUG - about to open ResultSet (open ResultSets: 0, globally: 0)
2006-03-12 23:24:49,328 [http-80-2] DEBUG - result set contains (possibly empty) collection: [isi.as.hib.objekt.Objekt.objektBilder#402882fb09f078fc0109f07a1b290001]
2006-03-12 23:24:49,328 [http-80-2] DEBUG - uninitialized collection: initializing
2006-03-12 23:24:49,328 [http-80-2] DEBUG - processing result set
2006-03-12 23:24:49,328 [http-80-2] DEBUG - result set row: 0
2006-03-12 23:24:49,328 [http-80-2] DEBUG - returning '402882fb09f078fc0109f07a87dd0003' as column: bilderId36_0_
2006-03-12 23:24:49,328 [http-80-2] DEBUG - result row: EntityKey[isi.as.hib.bilder.ObjektBild#402882fb09f078fc0109f07a87dd0003]
2006-03-12 23:24:49,328 [http-80-2] DEBUG - about to close ResultSet (open ResultSets: 1, globally: 1)
2006-03-12 23:24:49,328 [http-80-2] DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2006-03-12 23:24:49,328 [http-80-2] DEBUG - closing statement

Code:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 1:52 am 
Newbie

Joined: Sat Mar 11, 2006 7:12 pm
Posts: 3
Location: Oklahoma City
Hi Marc,

I got quite similar exception as you did. And I used hibernate 3.0.5, I thought hibernate 3.1.2 may solve the issue. Now it looks like it still have the issue. Not sure whether it is because we did not use it properly.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 3:45 am 
Newbie

Joined: Mon Nov 14, 2005 5:43 am
Posts: 6
Hi Oliver,

another user of hibernate wrote me that it could relate on the way hibernate
uses proxies on associations:

http://www.hibernate.org/hib_docs/v3/re ... mance.html
Section 19.1.13

May be it's that problem. I've got to look for it on the evening.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 3:56 pm 
Newbie

Joined: Mon Nov 14, 2005 5:43 am
Posts: 6
Just to be complete (and not to waste any credits):

I've found the point with the WrongClass. It's because Hibernate doesn't take care of discriminator columns if you've got to associations on one table wich is subclassed.

Example:

I've got an Entity A and to Entities B and C. B and C are subclasses of A and have an Discriminator Column on the same table.
If there is another Entity Y wich have to associations (@OneToMany) one on B and one on C hibernate ignores the discriminator column and returns the C-entities also on the B associations. After getting these the entities returned are of type B and then the association of C crashes...

So a good way: Delete the associations on B and C, do them on A and handle the association by hand.

An
@Where(clause="[discriminator-column] = '[discriminator-value]'")
is your friend...

I think that might be truely a bug... Should it go into JIRA?

me


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 1:56 am 
Newbie

Joined: Sat Mar 11, 2006 7:12 pm
Posts: 3
Location: Oklahoma City
Hi Mende,

Atually I found the similar bug already recorded in JIRA. I got a suggestion that I should consider use simple approach, say per table per class instead of using the discrimator. I have not got time to try it out, but I will let you know.

Thanks
Oliver


Top
 Profile  
 
 Post subject: So...
PostPosted: Thu Apr 13, 2006 7:41 pm 
Beginner
Beginner

Joined: Tue Dec 09, 2003 5:59 pm
Posts: 25
Location: Los Angeles
Anyone care to actually paste the bug number?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 13, 2006 8:27 pm 
Newbie

Joined: Thu Apr 13, 2006 8:00 pm
Posts: 2
I have a similar issue.

We have a versioned system thet I have entity E with oneToMeny to EVersions. then I have ETypeA and ETypeB extend E. ETypeAVersion and ETypeBVersion that extend EVersion.

Now I have another enitity B that has oneToMany to EVersions, when I call B.getEVersion,it returns my a set, however, when I tried to access the objects in the set, it throws me a wrongclassexception.

any suggestions?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.