Java Reference
In-Depth Information
Map names = new HashMap();
names.put("user","sample.entity.User");
names.put("advert","sample.entity.Advert");
names.put("category","sample.entity.Category");
List entities = document.getRootElement().content();
Iterator eit = entities.iterator();
while(eit.hasNext()) {
Node item = (Node)eit.next();
String entityName = (String)names.get(item.getName());
xmlSession.save(entityName,item);
}
session.getTransaction().commit();
session.close();
System.out.println("Done.");
}
}
Unfortunately, the default value for the node attribute, if it is not explicitly applied to the
class elements in the mapping files, is the short (unqualified) name of the class—not the entity
name, which defaults to the long (fully qualified) class name.
Other Considerations When Using XML Entities
The objects retrieved from Hibernate in the EntityMode.DOM4J session mode are Dom4J objects,
but they are still Hibernate persistence entities. You can therefore manipulate these entities
through the Dom4J API and persist these changes to the database without needing to access
the entity as a POJO.
Tools and APIs that process an XML document can therefore be applied to a Dom4J docu-
ment extracted from Hibernate, and as long as it is still associated with the originating session,
changes made to the document will be reflected in the database when the session is flushed or
the transaction is committed.
Hibernate and EJB 3 annotations do not provide support for the use of XML relational
persistence. If you want to use this feature to import or export data for an annotated applica-
tion, you will first need to generate appropriate XML-based mapping files from the annotated
classes.
Maps
In addition to the default mode (POJO) and the XML mode (Dom4J) described previously,
the Hibernate session can be accessed in one more way: as a map of name/value pairs. This
mode is accessed by calling the getSession() method with a parameter of EntityMode.MAP
(see Listing A-11).
Search WWH ::




Custom Search