Hibernate version: 3.2.1 GA
I want to serialize my Business objects (POJO's) to XML. Reading chapter 18 of the documentation explained how to do this, but it uses an XML-mapping file (which I'd rather not use, since I prefer annotations).
My first question is:
How to retrieve an XML representation of a POJO when I only use annotated classes?
If I try this:
Code:
Session session = HibernateUtil.getSession();
Session dom4jSession = session.getSession( EntityMode.DOM4J );
Element userXML = (Element)dom4jSession.load( Mask.class, 1l );
try {
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter( System.out, format );
writer.write( userXML );
} catch( IOException ex ) {
throw new RuntimeException( ex );
}
I end up with this exception:
Code:
Exception in thread "main" org.hibernate.HibernateException: No tuplizer found for entity-mode [dom4j]
at org.hibernate.tuple.EntityModeToTuplizerMapping.getTuplizer(EntityModeToTuplizerMapping.java:68)
at org.hibernate.tuple.entity.EntityMetamodel.getTuplizer(EntityMetamodel.java:100)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3403)
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:257)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:191)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:795)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:788)
at com.example.test.Tester.main(Tester.java:21)
This is my hibernate.cfg.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- Test commit over SSH -->
<session-factory>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.password">passwd</property>
<property name="connection.url">jdbc:mysql://localhost/processes</property>
<property name="connection.username">user</property>
</session-factory>
</hibernate-configuration>
But the testing code works when I add this line to the above configuration:
Code:
<mapping resource="Mask.hbm.xml"/>
The content of Mask.hbm.xml is as follows:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.example.dbo" default-access="field">
<class name="Mask">
<id name="id" type="long">
<generator class="native"/>
</id>
<property name="maskName"/>
</class>
</hibernate-mapping>
And to be fully clear, this is my HibernateUtil.java
Code:
package com.example.util.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import com.example.dbo.Mask;
public class HibernateUtil
{
private static final SessionFactory sessionFactory;
private static Session session = null;
static
{
try
{
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new AnnotationConfiguration()
.addPackage( "com.example.dbo" )
.addAnnotatedClass( Mask.class )
.configure()
.buildSessionFactory();
}
catch( Throwable ex )
{
System.err.println( "Initial SessionFactory creation failed." + ex );
throw new ExceptionInInitializerError( ex );
}
}
public static Session getSession()
{
if( session == null )
session = sessionFactory.openSession();
return session;
}
}
I have read chapter 3.4.2 from the book "Java persistance with Hibernate" but that does not make it clear if I can just use annotations or if I HAVE
to have .hbm.xml files in order to be able to use the EntityMode.DOM4J session.