-->
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.  [ 3 posts ] 
Author Message
 Post subject: Peristing XML
PostPosted: Wed Jul 19, 2006 3:17 pm 
Newbie

Joined: Wed Jul 19, 2006 10:44 am
Posts: 2
I am experimenting with persisting XML using the Dom4j capability.

I have table where all columns are string values. When I call saveOrUpdate, I get a class cast exception. The exception occurs when calling:

class org.hibernate.type.StringType{
...
public String toString(Object value) {
return (String) value;
}
...

Value is a DefaultDocument. I can understand why the would cause a CCE, what I don't understand is why value is not the contents of one of the elements I mapped to a column.

Hibernate version:
3.0
Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class entity-name="Event" table="EVENTS" node="event">
<id name="id" type="string" column="EVENT_ID" node="event/id"/>
<property name="title" type="string" column="TITLE" node="event/title" />
<property name="date" type="string" column="EVENT_DATE" node="event/date"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Session session = HibernateUtil.currentSession();
Session dom4jSession = session.getSession(EntityMode.DOM4J);
Transaction tx = session.beginTransaction();
Document doc = org.dom4j.DocumentHelper.createDocument();
try {
Element elEvent = doc.addElement("event") ;
Element elId = elEvent.addElement("id");
elId.setText("111");
Element elTitle = elEvent.addElement("id");
elTitle.setText("111-Title");
Element elDate = elEvent.addElement("id");
elDate.setText("01/01/2001");

} catch (Exception ex) {
ex.printStackTrace();
}

dom4jSession.saveOrUpdate("Event", doc);

tx.commit();
HibernateUtil.closeSession();

Full stack trace of any exception that occurs:
java.lang.ClassCastException
at org.hibernate.type.StringType.toString(StringType.java:42)
at org.hibernate.type.NullableType.toLoggableString(NullableType.java:122)
at org.hibernate.pretty.MessageHelper.infoString(MessageHelper.java:68)
at org.hibernate.persister.entity.BasicEntityPersister.getDatabaseSnapshot(BasicEntityPersister.java:860)
at org.hibernate.engine.PersistenceContext.getDatabaseSnapshot(PersistenceContext.java:302)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:189)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:408)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:82)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:464)
at XMLEvent.createAndStoreEvent(XMLEvent.java:48)
at XMLEvent.main(XMLEvent.java:26)

Name and version of the database you are using:
MySQL 5.0.19-nt
The generated SQL (show_sql=true):
DEBUG - Insert 0: insert into EVENTS (TITLE, EVENT_DATE, EVENT_ID) values (?, ?, ?)
DEBUG - Update 0: update EVENTS set TITLE=?, EVENT_DATE=? where EVENT_ID=?
DEBUG - Delete 0: delete from EVENTS where EVENT_ID=?


Debug level Hibernate log excerpt:
Found no debug level.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 3:35 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
You have:
Code:
Element elTitle = elEvent.addElement("id");
...
Element elDate = elEvent.addElement("id");

instead of:
Code:
Element elTitle = elEvent.addElement("title");
...
Element elDate = elEvent.addElement("date");

Is this a copy error?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 3:54 pm 
Newbie

Joined: Wed Jul 19, 2006 10:44 am
Posts: 2
Thankyou pepelnm for pointing out a coding error.

The problem remiains however.

When I said I encountered a CCE on the toString (see original post) call, that only happens when log4j is running DEBUG. When set to INFO, the CCE occurs on the call shown below. Once again, value is a DefaultDocument instead of a String.

class org.hibernate.type.StringType{
...
public void set(PreparedStatement st, Object value, int index) throws SQLException {
st.setString(index, (String) value);
}
}
...


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