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: