简体   繁体   中英

Generating Java-classes out of database with hibernate and maven - AnnotationConfiguration missing?

I want to create Java-classes out of an MySQL-Database using hibernate. Just using eclipse and the Hibernate-Plugin, this works fine (described here: http://www.wikihow.com/Generate-Hibernate-Pojo-Classes-from-DB-Tables ), but I want to do it with maven. This, after some tries, does not work.

Generelly, I have an hibernate.cfg.xml and a persistance.xml-file, both with the correct connection-information. I found some threads about howto generate the classes from java (for example How to configure hibernate-tools with maven to generate hibernate.cfg.xml, *.hbm.xml, POJOs and DAOs ) and the documentation of the hibernate-maven-plugin ( http://mojo.codehaus.org/hibernate3-maven-plugin ).

I tried several code snippets, the most promising seems to me from: Maven Java Source Code Generation for Hibernate

I added the file I need, and I got:

           <plugin>
             <groupId>org.codehaus.mojo</groupId>
             <artifactId>hibernate3-maven-plugin</artifactId>
             <version>2.2</version>
             <executions>
                <execution>
                  <phase>generate-sources</phase>
                  <goals>
                     <goal>hbm2java</goal>
                  </goals>
                </execution>
             </executions>
             <configuration>
                <components>
                  <component>
                     <name>hbm2java</name>
                     <implementation>configuration</implementation>
                     <outputDirectory>target/generated-sources/hibernate3</outputDirectory>
                  </component>
                </components>
                <componentProperties>
                  <drop>true</drop>
                  <jdk5>true</jdk5>
                  <configurationfile>/src/main/java/hibernate.cfg.xml</configurationfile>
             <packagename>de.unileipzig.database</packagename>
                </componentProperties>
             </configuration>
          </plugin> 

But unfortunately, when executing, I get

 [ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java (default-cli) on project AWV: Execution default-cli of goal org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java failed: An AnnotationConfiguration instance is required to use <mapping class="de.unileipzig.database.objectlist"/> -> [Help 1]

I googled the error, and found on http://www.mkyong.com/hibernate/hibernate-error-an-annotationconfiguration-instance-is-required-to-use/ that one has to add the dependency. It seemed somehow awkward to me, as I am using Hibernate 4 and the Maven Plugin for Hibernate 3 (the hibernate 4 plugin seems not to be practical usable for my case: http://www.smartics.eu/hibernate4-maven-plugin/ ), but I tried adding:

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>

(As the version specified at the Mykong-Post can't be found in my repositories).

Unfortunately, still, the error occurs. Does anybody have an hint how to solve this problem? Is there just an problem with the annotation-dependeny, or is my usage of the plugin not right?

After the advice of julschi, I added the following code to the plugin:

   <plugin>
         <dependencies>
                      <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>5.1.6</version>
                        </dependency>
                        <dependency>
                            <groupId>org.hibernate</groupId>
                            <artifactId>hibernate-core</artifactId>
                            <version>3.5.6-Final</version>
                        </dependency>
                        <dependency>
                            <groupId>org.hibernate</groupId>
                            <artifactId>hibernate-entitymanager</artifactId>
                            <version>3.5.6-Final</version>
                        </dependency>
                        <dependency>
                            <groupId>org.hibernate</groupId>
                            <artifactId>hibernate-annotations</artifactId>
                            <version>3.5.6-Final</version>
                        </dependency>
        </dependencies>
    </plugin> 

Unfortunately, this did not change anything. When I used the version I use in the project (Hibernate 4.2.7) it results in an error, that org.hibernate.util.StringHelper is not found; it seems to be moved to another package ( https://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/internal/util/StringHelper.html ). But if I use the version 3.5.6-FINAL, I just get the same AnnotationConfiguration error.

If someone wants to try it out: the whole POM is here: http://nopaste.info/a70449bee6.html .

maybe create a dependencies section in the plugin part itself, like this:

<plugin>
...
  <dependencies>
      <dependency>
         <groupId>com.h2database</groupId>
         <artifactId>h2</artifactId>
         <version>1.2.144</version>
      </dependency>
  </dependencies>
 ...
</plugin>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM