From: Stefan F. (JIRA) <no...@at...> - 2006-03-22 10:33:27
|
ClassCastException in CollectionType.toLoggableString if using CustomCollectionType ----------------------------------------------------------------------------------- Key: HHH-1586 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1586 Project: Hibernate3 Type: Bug Components: core Versions: 3.1.2, 3.1.3 Environment: Hibernate 3.1.2, Oracle 10g Reporter: Stefan Fellner Priority: Minor When implementing a UserCollectionType and setting the log-Level to debug, in CollectionType.toLoggableString a ClassCastException is thrown. I'll put a possible bugfix here: public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { if ( value == null ) return "null"; if ( Hibernate.isInitialized( value ) ) { if ( getReturnedClass().isInstance(value) ) { List list = new ArrayList(); Type elemType = getElementType( factory ); Iterator iter = getElementsIterator( value ); while ( iter.hasNext() ) { list.add( elemType.toLoggableString( iter.next(), factory ) ); } return list.toString(); } else { // this would be my solution if (value instanceof Element) { // for DOM4J "collections" only return ( (Element) value ).asXML(); //TODO: it would be better if this was done at the higher level by Printer } else { return value.toString(); } } } else { return "<uninitialized>"; } } The main problem is that UserCollectionType Interface doesn't contain getReturnedClass() method, so getReturnedClass().isInstance(value) will fail in case of UserCollectionTypes and in the else value is casted to org.dom4j.Element, while value is an instance of the UserCollectionType, which causes ClassCastException. -- 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 |
From: Steve E. (JIRA) <no...@at...> - 2006-07-05 15:03:00
|
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-1586?page=all ] Steve Ebersole updated HHH-1586: -------------------------------- Fix Version: 3.2.0.cr3 Assign To: Steve Ebersole Modified version will actually look like: public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { if ( value == null ) { return "null"; } else if ( !Hibernate.isInitialized( value ) ) { return "<uninitialized>"; } else { return renderLoggableString( value, factory ); } } protected String renderLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { if ( Element.class.isInstance( value ) ) { // for DOM4J "collections" only // TODO: it would be better if this was done at the higher level by Printer return ( ( Element ) value ).asXML(); } else { List list = new ArrayList(); Type elemType = getElementType( factory ); Iterator iter = getElementsIterator( value ); while ( iter.hasNext() ) { list.add( elemType.toLoggableString( iter.next(), factory ) ); } return list.toString(); } } Note that HHH-1881 also can now be used to effect this processing... > ClassCastException in CollectionType.toLoggableString if using CustomCollectionType > ----------------------------------------------------------------------------------- > > Key: HHH-1586 > URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1586 > Project: Hibernate3 > Type: Bug > Components: core > Versions: 3.1.3, 3.1.2 > Environment: Hibernate 3.1.2, Oracle 10g > Reporter: Stefan Fellner > Assignee: Steve Ebersole > Priority: Minor > Fix For: 3.2.0.cr3 > > > When implementing a UserCollectionType and setting the log-Level to debug, in CollectionType.toLoggableString a ClassCastException is thrown. > I'll put a possible bugfix here: > public String toLoggableString(Object value, SessionFactoryImplementor factory) > throws HibernateException { > if ( value == null ) return "null"; > > if ( Hibernate.isInitialized( value ) ) { > if ( getReturnedClass().isInstance(value) ) { > List list = new ArrayList(); > Type elemType = getElementType( factory ); > Iterator iter = getElementsIterator( value ); > while ( iter.hasNext() ) { > list.add( elemType.toLoggableString( iter.next(), factory ) ); > } > return list.toString(); > } else { > // this would be my solution > if (value instanceof Element) { > // for DOM4J "collections" only > return ( (Element) value ).asXML(); //TODO: it would be better if this was done at the higher level by Printer > } else { > return value.toString(); > } > } > } > else { > return "<uninitialized>"; > } > > } > The main problem is that UserCollectionType Interface doesn't contain getReturnedClass() method, so getReturnedClass().isInstance(value) will fail in case of UserCollectionTypes and in the else value is casted to org.dom4j.Element, while value is an instance of the UserCollectionType, which causes ClassCastException. -- 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 |
From: Steve E. (JIRA) <no...@at...> - 2006-07-05 15:06:56
|
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-1586?page=all ] Steve Ebersole resolved HHH-1586: --------------------------------- Resolution: Fixed > ClassCastException in CollectionType.toLoggableString if using CustomCollectionType > ----------------------------------------------------------------------------------- > > Key: HHH-1586 > URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-1586 > Project: Hibernate3 > Type: Bug > Components: core > Versions: 3.1.3, 3.1.2 > Environment: Hibernate 3.1.2, Oracle 10g > Reporter: Stefan Fellner > Assignee: Steve Ebersole > Priority: Minor > Fix For: 3.2.0.cr3 > > > When implementing a UserCollectionType and setting the log-Level to debug, in CollectionType.toLoggableString a ClassCastException is thrown. > I'll put a possible bugfix here: > public String toLoggableString(Object value, SessionFactoryImplementor factory) > throws HibernateException { > if ( value == null ) return "null"; > > if ( Hibernate.isInitialized( value ) ) { > if ( getReturnedClass().isInstance(value) ) { > List list = new ArrayList(); > Type elemType = getElementType( factory ); > Iterator iter = getElementsIterator( value ); > while ( iter.hasNext() ) { > list.add( elemType.toLoggableString( iter.next(), factory ) ); > } > return list.toString(); > } else { > // this would be my solution > if (value instanceof Element) { > // for DOM4J "collections" only > return ( (Element) value ).asXML(); //TODO: it would be better if this was done at the higher level by Printer > } else { > return value.toString(); > } > } > } > else { > return "<uninitialized>"; > } > > } > The main problem is that UserCollectionType Interface doesn't contain getReturnedClass() method, so getReturnedClass().isInstance(value) will fail in case of UserCollectionTypes and in the else value is casted to org.dom4j.Element, while value is an instance of the UserCollectionType, which causes ClassCastException. -- 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 |