-->
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.  [ 6 posts ] 
Author Message
 Post subject: ClassCastExcetpion on CustomUserType w/DOM4J
PostPosted: Thu Nov 24, 2005 7:42 pm 
Newbie

Joined: Sat Sep 17, 2005 9:19 pm
Posts: 3
Hibernate version:
3.1rc3

Mapping documents:

I've got a mapping to a CustomUserType as follows:

hibernate-mapping>
<joined-subclass
name="com.foo.model.primatives.LabeledSliderState"
table="LABELED_SLIDER_STATES" lazy="false"
extends="com.foo.model.AbstractState">
<key column="ID" />
<property name="nameLabel" node="nameLabel"
type="java.lang.String" />
<property name="testLabel"
type="com.foo.model.primatives.TestLabelState"
node="com.foo.model.primatives.TestLabelState">
<column name="testLabel"/>
</property>

</joined-subclass>
</hibernate-mapping>
TestLabelState implements CompositeUserType.
This works fine to get the instance of a LabeledSliderState saved. However, when I try to read it out using EntityMode.DOM4J, I get a ClassCastException:

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

Full stack trace of any exception that occurs:
java.lang.ClassCastException
at org.hibernate.type.CompositeCustomType.setToXMLNode(CompositeCustomType.java:250)
at org.hibernate.property.Dom4jAccessor$ElementSetter.set(Dom4jAccessor.java:310)
at org.hibernate.tuple.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:330)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3429)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:126)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
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:1493)
at org.hibernate.type.CollectionType.getCollection(CollectionType.java:501)
at org.hibernate.type.CollectionType.resolveKey(CollectionType.java:336)
at org.hibernate.type.CollectionType.resolve(CollectionType.java:329)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1791)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:107)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:81)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2911)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:161)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:812)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:750)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:743)
at com.foo.services.hibernate.HibernateService.showDeviceXML(HibernateService.java:101)
at com.foo.model.playbox.PlayboxStateFactory.createDeviceState(PlayboxStateFactory.java:39)
at com.foo.services.ui.UIBuilderService.createDevice(UIBuilderService.java:102)
at com.foo.designTool.Playbox.createUI(Playbox.java:154)
at com.foo.designTool.Playbox.main(Playbox.java:165)

The value object passed to setToXMLNode is not an 'org.dom4j.Element' but rather a 'TestLabelState'.

Is there something I'm missing w/in my hbm.xml mapping, or w/the implementation of TestLabelState ?

Name and version of the database you are using:
HSQLDB

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 9:15 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
try change

Code:
<property name="testLabel"
type="com.foo.model.primatives.TestLabelState"
node="com.foo.model.primatives.TestLabelState">


with

Code:
<property name="testLabel"
type="com.foo.model.primatives.TestLabelState"


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 9:25 pm 
Newbie

Joined: Sat Sep 17, 2005 9:19 pm
Posts: 3
Thanks, but no luck.
The object is still a TestLabelState, and not a DefaultElement.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 25, 2005 8:34 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
can you post the code of TestLabelState ?

_________________
dont forget to rate !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 25, 2005 11:11 am 
Newbie

Joined: Sat Sep 17, 2005 9:19 pm
Posts: 3
/*
* Created on Oct 12, 2005 @author : String
*/
package com.foo.model.primatives;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.type.Type;
import org.hibernate.usertype.CompositeUserType;
import com.foo.model.AbstractState;
import com.foo.model.IControllerState;

/**
* @author String
*
*/
public class TestLabelState extends AbstractState implements IControllerState, CompositeUserType{
private String labelText = "";
/**
*
*/
public TestLabelState() {
super();
}
/**
* @param id2
*/
public TestLabelState(Integer id2) {
super(id2);
}
public TestLabelState(String text) {
this(null, text);
}
public TestLabelState(Integer id2, String text) {
super(id2);
setLabelText(text);
}
public String getLabelText() {
return labelText;
}
public void setLabelText(String text) {
if (this.labelText.equals(text)) {
return;
}
this.labelText = text;
notifyObservers(createValueChangeEvent());
}
/**
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return null;
}
/**
* @see org.hibernate.usertype.UserType#returnedClass()
*/
public Class returnedClass() {
return TestLabelState.class;
}
/**
* @see org.hibernate.usertype.UserType#equals(java.lang.Object,
* java.lang.Object)
*/
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) {
return true;
}
if (x == null || y == null) {
return false;
}
TestLabelState lsx = (TestLabelState) x;
TestLabelState lsy = (TestLabelState) y;
return lsx.getLabelText().equals(lsy.getLabelText());
}
/**
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(Object x) throws HibernateException {
return ((TestLabelState)x).getLabelText().hashCode();
}
/**
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(Object value) throws HibernateException {
return new TestLabelState(((TestLabelState)value).getLabelText());
}
/**
* @see org.hibernate.usertype.UserType#isMutable()
*/
public boolean isMutable() {
return true;
}
/**
* @see org.hibernate.usertype.CompositeUserType#getPropertyNames()
*/
public String[] getPropertyNames() {
return new String[] { "labelText" };
}
/**
* @see org.hibernate.usertype.CompositeUserType#getPropertyTypes()
*/
public Type[] getPropertyTypes() {
return new Type[] { Hibernate.STRING };
}
/**
* @see org.hibernate.usertype.CompositeUserType#getPropertyValue(java.lang.Object,
* int)
*/
public Object getPropertyValue(Object component, int property) throws HibernateException {
TestLabelState ls = (TestLabelState) component;
return ls.getLabelText();
}
/**
* @see org.hibernate.usertype.CompositeUserType#setPropertyValue(java.lang.Object,
* int, java.lang.Object)
*/
public void setPropertyValue(Object component, int property, Object value) throws HibernateException {
((TestLabelState) component).setLabel((String) value);
}
/**
* @see org.hibernate.usertype.CompositeUserType#nullSafeGet(java.sql.ResultSet,
* java.lang.String[], org.hibernate.engine.SessionImplementor,
* java.lang.Object)
*/
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
throws HibernateException, SQLException {
String text = (String)Hibernate.STRING.nullSafeGet(rs, names[0]);
if (text == null){
return null;
}

return new TestLabelState(text);
}
/**
* @see org.hibernate.usertype.CompositeUserType#nullSafeSet(java.sql.PreparedStatement,
* java.lang.Object, int, org.hibernate.engine.SessionImplementor)
*/
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
throws HibernateException, SQLException {
Hibernate.STRING.nullSafeSet(st, ((TestLabelState)value).getLabelText(), index);
}
/**
* @see org.hibernate.usertype.CompositeUserType#disassemble(java.lang.Object,
* org.hibernate.engine.SessionImplementor)
*/
public Serializable disassemble(Object value, SessionImplementor session) throws HibernateException {
return (Serializable)deepCopy(value);
}
/**
* @see org.hibernate.usertype.CompositeUserType#assemble(java.io.Serializable,
* org.hibernate.engine.SessionImplementor, java.lang.Object)
*/
public Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException {
return deepCopy(cached);
}
/**
* @see org.hibernate.usertype.CompositeUserType#replace(java.lang.Object,
* java.lang.Object, org.hibernate.engine.SessionImplementor,
* java.lang.Object)
*/
public Object replace(Object original, Object target, SessionImplementor session, Object owner)
throws HibernateException {
return deepCopy(original);
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 28, 2005 5:06 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
Just a general suggestion,
try to create two classes:

TestLabelState as your business class
and TestLabelStateType as your UserType.
Only TestLabelStateType should implement CompositeUserType
and:

Code:
   public Class returnedClass() {
      return TestLabelState.class;
   }



Another suspicious function is:
Code:
public int[] sqlTypes() {
return null;
}


The documentation about this function reads:

Quote:
/**
* Return the SQL type codes for the columns mapped by this type. The
* codes are defined on <tt>java.sql.Types</tt>.
* @see java.sql.Types
* @return int[] the typecodes
*/


Perhaps this helps.

_________________
dont forget to rate !


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