7 Replies Latest reply on Feb 10, 2008 5:05 AM by baz

    How to generate annotated Java classes from hbm.xml files

    baz

      Hello,
      we have a couple of old projects. The mapping is described in hbm.xml files.
      We wish to migrate this to annotated Java classes.
      We performed theses steps:
      1. We generated a hibernate console configuration for our project.

      2. opening hibernate perspective

      3. Run / hibernate code generation

      4. filling out the dialog on the main tab choosing the console configuration and selecting an output directory
      on the exporters tab selecting Java 5 syntax and domain code generation

      5 Run

      Code is generated but without annotations. so we specified in step 4 generate ejb3 annotations. But this results in the following error and no code is generated.

      org.hibernate.tool.hbm2x.ExporterException: Error while processing template pojo/Pojo.ftl
      Error while processing template pojo/Pojo.ftl
       freemarker.core.InvalidReferenceException: Expression pojo.generateManyToOneAnnotation is undefined on line 11, column 3 in pojo/Ejb3PropertyGetAnnotation.ftl.
       Expression pojo.generateManyToOneAnnotation is undefined on line 11, column 3 in pojo/Ejb3PropertyGetAnnotation.ftl.
       freemarker.core.InvalidReferenceException: Expression pojo.generateManyToOneAnnotation is undefined on line 11, column 3 in pojo/Ejb3PropertyGetAnnotation.ftl.
       Expression pojo.generateManyToOneAnnotation is undefined on line 11, column 3 in pojo/Ejb3PropertyGetAnnotation.ftl.
      

      We are using JBoss Tolls 2.0.0GA
      The projects we are trying to convert are JSF Projects created with the former Exadel studio.

      Is it possible to convert our hbm.xml files to annotatd lava classes?

        • 1. Re: How to generate annotated Java classes from hbm.xml file
          maxandersen

          so you are probably using some specific construct in hbm.xml that the annotations generation does not support yet. If you can figure out which hbm.xml or set of hbm.xml's that is causing the issue put them in jira and we can look at what is the problem.

          • 2. Re: How to generate annotated Java classes from hbm.xml file
            baz

            Thanks for the response.
            Meanwhile i have detected that code IS generated. Due to the error there is no refresh of the directory list inside eclipse. A simple refresh revealed the generated code.
            All classes are there. But due to the error a many2one dependenxy must be missing. I will investigate further and compare the genereted Code with my expectations.

            Some questions?
            Is code generated class by class?
            Or it is generated in turns,e.g.: first all the java code for all classes, then table annotations for all classes, then dependencys ...

            I am asking because i am missing named querys which are present in the hbm.xml but not in the generated code.
            Perhaps the error occurs when these querys are parsed.
            Ciao,
            Carsten

            • 3. Re: How to generate annotated Java classes from hbm.xml file
              maxandersen

              They are generated per class.

              Im not sure named queries are generated by the annotation support...

              • 4. Re: How to generate annotated Java classes from hbm.xml file
                baz

                Hello,
                Thanks for the answer.
                I am now trying to assemble a short example wich shows the problematic code. I am not quite sure that i found it already. But a one2one association is not correctly generated so that the generated code gives a compiler error. If i am ready i submit it to jira with reference to this thread.

                • 5. Re: How to generate annotated Java classes from hbm.xml file
                  baz

                  I have created this JIRA issue: http://jira.jboss.com/jira/browse/JBIDE-1767

                  • 6. Re: How to generate annotated Java classes from hbm.xml file
                    baz

                    When igenerate source code with the mapping files in the JIRA issue no exception is thrown. It only generates faulty code. So the exception must come from another part of the code:-( I will investigate this further.

                    • 7. Re: How to generate annotated Java classes from hbm.xml file
                      baz

                      Finally i found it.
                      This mapping causes the exception:

                      <?xml version="1.0" encoding="UTF-8"?>
                      <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
                      
                      <hibernate-mapping package="demo">
                       <class name="Sample2Well" table="sample2well" optimistic-lock="none">
                       <id name="id" type="long" unsaved-value="null" column="SAMPLE2WELLID">
                       <generator class="native"/>
                       </id>
                       <property name="analysisIndex" type="integer" column="analysisindex"/>
                       <bag name="sampleWell" lazy="false" table="samplewell" cascade="save-update">
                       <key foreign-key="sampleWell_id" column="SAMPLE2WELLID"/>
                       <composite-element class="demo.SampleWellType">
                       <many-to-one name="sample" entity-name="demo.Sample" foreign-key="Sample_id" not-null="false" column="SAMPLEID" cascade="none"/>
                       <nested-composite-element name="well" class="demo.Well">
                       <property name="adress" type="string" column="well_adress"/>
                       <property name="column" type="short" column="well_column"/>
                       <property name="row" type="character" column="well_row" length="1"/>
                       </nested-composite-element>
                       </composite-element>
                       </bag>
                       </class>
                      </hibernate-mapping>
                       <hibernate-mapping package="demo">
                       <class name="Sample" table="sample" optimistic-lock="none">
                       <meta attribute="class-description" inherit="false">@hibernate.class table="sample"</meta>
                       <id name="id" type="long" unsaved-value="null" column="SAMPLEID">
                       <generator class="native"/>
                       </id>
                       <version name="version" column="version"/>
                       <property name="sampleCode" type="string" column="sample_code"/>
                       </class>
                      </hibernate-mapping>
                      

                      I have created this second jira issue:
                      http://jira.jboss.com/jira/browse/JBIDE-1768