EJB3 XML mapping files

The latest release of Hibernate EntityManager (3.1.0 beta8) now support EJB3 XML mapping files (aka deployment descriptors). While annotations are considered a huge step forward for development ease-of-use and productivity, some folks out there stayed concerned about the ability to split metadata from the code. This is now supported in Hibernate Annotations (3.1.0 beta 10) and Hibernate EntityManager in a standard manner. You can partially overrides java annotations or you can write all you metadata through XML. The easiest solution is to add META-INF/orm.xml in your ejb-jar or persistence jar. This file is automatically taken into account by the persistence provider. Alternatively you can add a <mapping-file/> element to your persistence.xml file. Here is a sample for EJB3 XML file

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="[=>http://java.sun.com/xml/ns/persistence/orm]"
               [=>xsi:schemaLocation=]"=>http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
  <entity class="Car" metadata-complete="true" access="PROPERTY">
      <table name="CARS"/>
      <named-query name="carsByManufacturer">
          <query>select c from Car c where c.manufacturer = :manufacturer</query>
          <hint name="org.hibernate.timeout" value="200"/>
          <id name="id">
              <generated-value strategy="AUTO"/>
              <column name="car_id"/>
          <basic name="category" optional="false">
          <many-to-one name="manufacturer">
              <join-column name="manufacturer_fk"/>

Glassfish integration

I have been working with the Glassfish team for some times now to make sure Hibernate EntityManager integrates well with Glassfish. Now that the specification has been frozen, the integration is working very smoothly:

  • download Hibernate Core (for HEM 3.1.0 beta 8, you will need Hibernate 3.2 CR1)
  • copy the required hibernate (and third party libs) into $glassfish_home/lib
  • adjust your persistence.xml file to use Hibernate EntityManager
<persistence version="1.0">
    <persistence-unit name="pu">
        <!-- JNDI name of the database resource to use -->
            <!-- drop and create tables at deployment if needed -->
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

That's it! Glassfish sets the appropriate Transaction Manager Strategy and Hibernate can guess the appropriate Dialect for most common databases.

Hibernate EntityManager is know to run in all up-to-date EJB3 implementations. Of course, it runs smoothly in JBoss EJB3 and JBoss Embeddable EJB3 .

Mapping of Maps

This has been a long time requested feature, it is now available, you can map you associations using Map<Element, ...> or Map<Embeddable, ...> using an explicit key column(s)

Back to top