0

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?

Tiny
  • 27,221
  • 105
  • 339
  • 599
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • @lexicore You do not yet support eclipse. Thank you for what you have made available to the Maven community. If I am able to get hyperjaxb to work in eclipse, I will let you know. – CodeMed Oct 10 '14 at 20:34
  • 1
    "You do not yet support eclipse" is a false claim. – lexicore Oct 13 '14 at 20:29
  • JAXB runs perfectly easily without requiring installation support because it provides architecture-level support that avoids complicated configuration. Questions from eclipse users give an opportunity for subsequent generations of hyperjaxb to be designed in a way that avoids the need for complicated ports to eclipse. In the meantime, I have another question to hopefully finish the work of installing all the functionality of the current generation of hyperjaxb into eclipse: http://stackoverflow.com/questions/26348480/persisting-hyperjaxb-generated-entities-to-mysql-from-eclipse – CodeMed Oct 13 '14 at 21:10
  • Link(http://confluence.highsource.org/display/HJ3/Purchase+Order+Tutorial) provided in above question details in not working anymore. Any idea about new link for same ?? – Pushpendra Apr 25 '16 at 07:00
  • 1
    @Pushpendra Have a look at Spring Data JPA instead of hyperjaxb. The reason the hyperjaxb links are dead is that hyperjaxb was just a hobby project from one developer with a few users. By contrast, Spring Data is backed by a huge community and can automate all the translation of Java entities in and out of databases. Write your JAXB code to get get XML into entities, then let Spring Data do the ORM setup. And override Spring Data defaults to customize it. – CodeMed Apr 25 '16 at 17:44
  • @CodeMed thanks :) – Pushpendra Apr 26 '16 at 06:09

2 Answers2

5

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:

Tutorial project in the Eclipse workspace

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.

Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221
0

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.

CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • 1
    The claim that Hyperjaxb does support not Eclipse is incorrect and misleading. Hyperjaxb is simply a code generation tool. Hyperjaxb's Maven projects are built in full accordance with standard Maven conventions and surely do work in Eclipse. The task of importing a Hyperjaxb project into Eclipse is simply and exactly the task of importing a standard Maven project into Eclipse. It is just a matter of a correct and actual M2Eclipse configuration in Eclipse and correctly executed import of an existing Maven project. Sometimes it fails, but it has nothing to do with Hyperjaxb or its architecture. – lexicore Oct 13 '14 at 20:28
  • The hyperjaxb architecture is not agnostic in the way that the JAXB architecture architecture is agnostic. As a result, JAXB-generated classes can be dragged and dropped easily into any eclipse project, while hyperjaxb-generated classes require complex reconfiguration of an eclipse project. This is a simple observation and should not be taken personally. Relax. This is valuable feedback that can help you avoid having hyperjaxb functionality simply replaced by more agnostic upgrades in the next generation of JAXB. No offense is intended. – CodeMed Oct 13 '14 at 20:42
  • 1
    "The hyperjaxb architecture is not agnostic in the way that the JAXB architecture architecture is agnostic." This is a false claim. Hyperjaxb just enhances JAXB-annotated classes with JPA annotations. This has nothing to do with Eclipse whatsoever. – lexicore Oct 13 '14 at 20:49
  • When a user can simply import `hyperjaxb`-generated classes into an eclipse source folder, add relevant dependencies to the `pom.xml` file, run a maven update, and have everything work immediately, then it will be agnostic in the way that `jaxb`-generated classes are agnostic. – CodeMed Oct 13 '14 at 20:51