How to migrate from hibernate from 4.3 to 5.x?

Hi,

Currently, my project is using xdoclet (2.0.1) and hibernate (4.3.10) to generate *.hbm.xml, and export SQL. I have tried to upgrade these libraries to the latest version, especially for hibernate to version 5.4. I can generate hbm files with the new version of hibernate, but then it is failed to export schema using SchemaExportTask with the following error. I assume that I need to upgrade other libraries to make it work, but I have not figured it out yet. Any suggestions for this issue, I really appreciate.

Error performing export: Could not parse mapping document: D:…\model\AclTemplate.hbm.xml (FILE)

Ant task for generating hbm and SQL files

Some libraries (jar files) are being used for the generation

  • xdoclet-2.0.1.jar
  • xdoclet-plugin-hibernate-1.0.1.jar
  • hibernate-commons-annotations-4.0.5.Final.jar
  • hibernate-core-4.3.10.Final.jar

Thanks
Khoa Tran

The stacktrace should tell you what is wrong with the hbm.xml file you generate. It’s a parsing error.

Actually, I did add -verbose or -debug option when executing ant target, but I only got the error as I mentioned above. That is the reason I have no clue to investigate further, everything works properly if using hibernate-core-4.x. I have read some migration documents, and there are some constructors of SchemaExport class that have been deprecated, so I assume it might be the issue, is that correct? Could we still use SchemaExportTask together with hibernate 5.x, right ? Thanks for your response.

I don’t know how to help you if you can’t properly determine a stack trace with the way you invoke Hibernate. You can surely use SchemaExportTask with 5.x, but the error clearly says that there was an error during parsing of the hbm.xml, so you will have to look into that.
Why don’t you debug into SchemaExportTask and try to find out what the error is about?

@tqdkhoa : Can you share sample hbmxml file and the stack trace?

Hi @ashuspeed,

Below is the stack trace for generating hbm and schema export. There are two ant targets, including java2hbm and build.db.schema. The hbm files are generated successfully and placed in the mapping directory, then the next ant target build.db.schema will export to ddl. But I got the issue above, I’m debugging the SchemaExportTask as @beikov 's suggestion, but for now, I have not found the root cause yet. I would appreciate your suggestions. Thanks

java2hbm:
     [echo]  +++ Generate *.hbm.xml sybase mapping files for model classes +++ 
   [delete] Deleting directory D:\src\com\example\mapping\sybase
    [mkdir] Created dir: D:\src\com\example\mapping\sybase
    [mkdir] Created dir: D:\src\com\example\hbm_temp
     [copy] Copying 1 file to D:\src\com\example\build\conf
     [copy] Copying 1 file to D:\src\com\example\build\conf
     [copy] Copying 1 file to D:\src\com\example\build\conf
     [echo]  Generating *.hbm.xml files 
[hibernatedoclet] Running org.xdoclet.plugin.hibernate.HibernateMappingPlugin
[hibernatedoclet]   * Generate mapping for 'AclTemplate' entity
[hibernatedoclet]   * Generate mapping for 'AclTemplateElement' entity
[hibernatedoclet]   * Generate mapping for 'AddressBlock' entity
[hibernatedoclet]   * Generate mapping for 'AddressRange' entity
[hibernatedoclet]   * Generate mapping for 'AddressTemplate' entity
[hibernatedoclet]   ...
     [copy] Copying 264 files to D:\src\com\example\mapping\sybase
     [copy] Copying 264 files to D:\src\com\example\build\WEB-INF\classes
   [delete] Deleting directory D:\src\com\example\hbm_temp
     [echo]  --- *.hbm.xml mapping generation complete for sybase--- 
build.db.schema:
     [echo]  Calling build.schema with params:
     [echo]             dbtype="sybase"
     [echo]             userName="admin"
     [echo]             password="admin"
     [echo]             serverName="SYBASE"
     [echo]             table.sql="table.sql"
     [echo]             const.sql="drop_constraint.sql"
     [echo]             sqldir="D:\src\com\example/sql/sybase"
     [echo]             propFile="D:\src\com\example/conf/hibernate.properties"
     [echo]         
     [echo]  Building DB Schema for sybase 
     [echo] Generating Hibernate Schema for sybase to D:\src\com\example/sql/sybase/table.sql using D:\src\com\example/conf/hibernate.properties
[schemaexport] ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

BUILD FAILED
D:\src\com\example\build.xml:3296: The following error occurred while executing this line:
D:\src\com\example\build.xml:830: The following error occurred while executing this line:
D:\src\com\example\build.xml:608: Error performing export : Could not parse mapping document: D:\src\com\example\mapping\sybase\model\AclTemplate.hbm.xml (FILE)

AclTemplate.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class dynamic-update="true" table="acl_template" dynamic-insert="true" name="com.example.model.AclTemplate">
    <id unsaved-value="0" name="id" column="acl_template_id">
      <generator class="com.example.hibernate.StoredProcIdGenerator">
        <param name="OWNER_TYPE_DESCR">ACL_TEMPLATE</param>
      </generator>
    </id>
    <version unsaved-value="undefined" name="version" column="version" type="long"/>
    <property name="name">
      <column name="name" length="255" not-null="true"/>
    </property>
    <many-to-one cascade="none" foreign-key="FK_ACL_TEMPLATE_ORGANIZATION" class="com.example.model.Organization" name="organization">
      <column name="org_id" not-null="true"/>
    </many-to-one>
    <property name="internalView">
      <column name="internal_view" not-null="true"/>
    </property>
    <component name="lastUpdate" class="com.example.model.LastUpdate">
      <property name="user">
        <column name="last_upd_usr"/>
      </property>
      <property name="date">
        <column name="last_upd_dt" sql-type="datetime"/>
      </property>
    </component>
    <set inverse="true" cascade="none" order-by="sort_order" lazy="true" table="acl_template_element" name="aclTemplateElements">
      <key column="acl_template_id"/>
      <one-to-many class="com.example.model.AclTemplateElement"/>
    </set>
  </class>
</hibernate-mapping>

Thanks
Khoa Tran

@tqdkhoa : i checked the error details and it looks like the error has initiated from
[schemaexport] ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console…

and lower down it says error parsing xml.Just in case you want to fetch the actual cause for error, You can write a code in java where-in you can pass the hbmxml file to get the detailed stacktrace if its failing while parsing the file or somewhere else.

Alternatively Hibernate also provide the test-case template which you can fork and supply the same hbmxl file and run the test-case to check what SQL Queries are generated.

Reference Test-Case template to check the difference in versions 4 and 5 are located at