Hibernate version:
3.2.4.sp1
Mapping documents:
Code:
<hibernate-mapping>
<class name="de.uni_leipzig.lots.common.objects.News" table="news" node="news">
<id name="id" column="id" type="long" unsaved-value="null" node="@id">
<generator class="increment"/>
</id>
<property name="headline" length="255" not-null="true"/>
<property name="message" type="text" not-null="true"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session dom4jSession = session.getSession(EntityMode.DOM4J);
Element xmlNews = dom4jSession.get(News.class, id);
StringWriter writer = new StringWriter();
XMLWriter xmlWriter = new XMLWriter(writer, DOM4JHelper.getPrettyPrintOutputFormat());
xmlWriter.write(xmlNews);
xmlWriter.close();
Full stack trace of any exception that occurs:--
Name and version of the database you are using:--
The generated SQL (show_sql=true):--
Debug level Hibernate log excerpt:--
My problem is, that whitespaces in the message string of the news are not preserved in the XML output. I use DOM4J as backup/restore of all my entities. The last unsolved problem is that whitespaces are not preserved.
With whitespaces I mean line breaks and normal spaces. For some strings of my entities it is important to preserve those.
What do I get as XML?
Code:
<news id="1">
<headline>Testnews</headline>
<message>First line Second line Third line</message>
</news>
What do I want?
Code:
<news id="1">
<headline>Testnews</headline>
<message><![CDATA[First line
Second line
Third line]]></message>
</news>
or
Code:
<news id="1">
<headline>Testnews</headline>
<message xml:space="preserve">First line
Second line
Third line</message>
</news>
Hibernate produces a org.dom4j.tree.DefaultText node for the message text which includes the string "First line\nSecond line\nThird line" which is right. But the XMLWriter from dom4j trims this line breaks, because the text node is no CDATA and the sourrounding <message> node does not have the xml:space="preserve" attribute.
So hibernate should add the xml:space="preserve" attribute to the <message> node or generate a CDATA text node if I want preserve whitespaces for a particular property. So we need a possibility to declare a property to be space preserving in DOM4J mode.
What do you think about this? Should a file a feature request?
Thanks
Alexander Kiel
Univeristy of Leipzig, Germany