Importing hyperjaxb purchase order tutorial into Eclipse

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 05:46:43

问题


I would like to import the sample app from this tutorial into eclipse. I keep getting errors related to where the files get placed. When I create a Main class to run the code in the tutorial, I either get compilation errors from Eclipse not seeing the required packages when the Main class is in the build path, or I get a

Launch Error: Section does not contain a main type

when the Main class is not in the build path.

So far, I have taken the following steps:

  1. Download zip
  2. Navigate to folder in cmd.exe
  3. Run mvn clean install
  4. Create new maven project in eclipse
  5. Add the below to the pom.xml
  6. Run Maven update project in eclipse
  7. Create target/generated-sources/xjc folder
  8. Import the files generated by hyperjaxb into target/generated-sources/xjc
  9. Create a Main class elsewhere in the eclipse project to run test code
  10. Start adding code from the "Working with JAXB and JPA" section of the tutorial to the Main class

The above steps result in the errors I described above. What are step by step instructions for getting this to work in Eclipse?


回答1:


Ok, this is gonna be lengthy.

First of all, let's fix where we start. In this question we have finally figured out that the tutorial is workin fine, there were just some difficulties finding the generated Java code.

As a side note, this tutorial is a Maven project which uses the maven-hyperjaxb3-plugin. In Maven standard convention is to generate code in target\generated-sources\myTool. JAXB's tool for code generation is called XJC so the standard convention for schema-derived code is target\generated-sources\xjc. maven-jaxb2-plugin does it as well.

So from now on I assume that the PO tutorial worked fine: the code was generated, roundtrip test ran with the HSQLDB database etc.

What you are asking now is basically three thing:

  • How to switch to MySQL?
  • How to generate database schema with hbm2ddl?
  • How to import the project into Eclipse?

Please note that NONE of this is really specific to Hyperjaxb.

Let's get started.

Switching to MySQL

First of all, you have to replace HSQLDB with MySQL in the pom.xml. Remove this:

    <dependency>
        <groupId>hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>1.8.0.7</version>
        <scope>test</scope>
    </dependency>

And add this:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.0.5</version>
        <scope>test</scope>
    </dependency>

Next, edit src/test/resources/persistence.properties. Replace this:

hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.url=jdbc:hsqldb:target/test-database/database
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
hibernate.jdbc.batch_size=0

With this:

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.username=...
hibernate.connection.password=...
hibernate.connection.url=jdbc:mysql://localhost/hj3
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
hibernate.jdbc.batch_size=0

I personally don't have a MySQL database at hand at the moment, so I can't really test the roundtrip. Therefore I'll comment out

<!--roundtripTestClassName>RoundtripTest</roundtripTestClassName-->

in pom.xml.

If you have a database at hand, just configure the right URL/username/password in the mentioned persistence.properties file.

At this point your Maven project is reconfigured to use MySQL. If the roundtrip test is not commented out and the database is available, the roundrip test should run with the DB, i.e. create the schema, import the sample XML, read it back and compare alpha and omega.

So now we have the tutorial on MySQL and can move on.

Generating the database schema

This was a tricky part to figure out.

In order to generate the database schema in a file, you have to use the hbm2ddl tool. There are Maven plugins for that, in case of Hibernate 3 it seemed that the Codehaus plugin is the leading one. Finally, I have figured out the following configuration. You have to add the following plugin to your pom.xml (project/build/plugins):

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>hibernate3-maven-plugin</artifactId>
            <version>3.0</version>
            <executions>
                <execution>
                    <id>generate-schema</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <hibernatetool>
                    <classpath>
                        <path location="${project.build.directory}/classes" />
                    </classpath>

                    <jpaconfiguration persistenceunit="org.jvnet.hyperjaxb3.ejb.tests.pocustomized" propertyfile="src/test/resources/persistence.properties"/>

                    <hbm2ddl export="false" create="true" update="false" format="true" outputfilename="schema.ddl" />

                </hibernatetool>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate.javax.persistence</groupId>
                    <artifactId>hibernate-jpa-2.0-api</artifactId>
                    <version>1.0.0.Final</version>
                </dependency>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-entitymanager</artifactId>
                    <version>3.6.5.Final</version>
                </dependency>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.0.5</version>
                </dependency>
            </dependencies>
        </plugin>

Few things are important:

  • Hyperjaxb3 generates JPA annotations, so you have to use jpaconfiguration.
  • Therefore the hibernate3-maven-plugin must be executed in the compile phase (you need classes to read annotations from so they have to be compiled at that moment).
  • You have to include the compiled classes (${project.build.directory}/classes) to the hibernatetool's classpath so that it can discover classes and read annotations.
  • You have to let the hibernatetool know where you find your Hibernate properties (propertyfile="src/test/resources/persistence.properties").
  • Finally you have to let it know, which persistence unit you want to process (persistenceunit="org.jvnet.hyperjaxb3.ejb.tests.pocustomized"). Take a look at target/generated-sources/xjc/META-INF/persistence.xml.
  • Finally, add all the required dependencies.

Finally you arrive at the configuration I posted above. At this point the build should also generate the database schema in target/sql/hibernate3/schema.ddl. This was the second question.

So now we're done with the Maven project, let's switch to Eclipse.

Importing project into Eclipse

What I personally don't understand is things like

8.) import the files generated by hyperjaxb into target/generated-sources/xjc

Hyperjaxb3 just generate Java files in this directory (in packages). So you don't import anything anywhere. Do not try to copy files manually.

The right way to do this is to use the standard m2e plugin in Eclipse and, well, just import the Maven project into Eclipse workspace.

File > Import... > Existing Maven Projects > Root Directory > (Navigate to the directory containing pom.xml of the tutorial project) > (Check on pom.xml) > Finish

No you'll have a project in Eclipse. Eclipse will probably complain about unnsupported plugin goal execution for hibernate3-maven-plugin. (Eclipse does not know, how to integrate this plugin into Eclipse's build cycle.) Choose "Do not execute (add to pom)". This will add an "ignore" configuration to the pom.xml and make Eclipse happy.

Now refresh the project with F5. And you're done.

It might be that Eclipse did not pick everything at once, so maybe you'll need to update the Maven project (Alt+F5). This is how it looks in my workspace right now:

No errors, everything is on place. Nice and clean.

If your database is set up correctly (in persistence.properties), and you've generated a roundtrip test, you can execute it directly (RoundtripTest > Run As > JUnit Test). This will start the entity manager, load the XML, save it to the DB, load it back and compare to the original.

Now we're done.

The import of the Maven project which I described above is also in no way Hyperjaxb3-specific.


No please let me address your "step by step" list:

1.) download zip 2.) navigate to folder in cmd.exe 3.) run mvn clean install

Ok.

4.) create new maven project in eclipse

Why a new one?

5.) add the below to the pom.xml

Does not work. You had to figure out the right configuration of the hbm2ddl, just like I did. This is not Hyperjaxb3-speicifc.

6.) run Maven update project in eclipse

Ok.

7.) create target/generated-sources/xjc folder

No. Everything under target is generated, you never create anything manually there.

8.) import the files generated by hyperjaxb into target/generated-sources/xjc

I have no idea what you mean here. were you trying to copy the files manually?

9.) create a Main class elsewhere in the eclipse project to run test code
10.) Start adding code from the "Working with JAXB and JPA" section of the tutorial to the Main class

I wish you good luck and success with your projects.




回答2:


The solution involved right-clicking on various folders in the eclipse project, choosing Build Path.. and then using trial and error to select either Use as Source Folder, Configure Build Path, or Remove from Build Path for various folders in an attempt to recreate the build path required by the project's architecture. Some of the concepts in lexicore's answer were helpful, but ultimately required manual build path changes in eclipse before the app could marshal without throwing errors.

There must be a way for the architecture of hyperjaxb to be evolved so that these manual steps are not required. I would imagine that changing the pom to define the build path properly would be a short term solution. JAXB allows generated code to be imported into eclipse without all these manual configuration requirements, perhaps because the code in the JAXB jars may be agnostic with respect to directory structure.



来源:https://stackoverflow.com/questions/26286055/importing-hyperjaxb-purchase-order-tutorial-into-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!