Enterprise Java Development@TOPIC@
The project created from this archetype has two mechanisms for managing schema; maven plugin-based and EntityManager-based. Know that when you start this exercise the two mechanisms may be both activated and competing (last one wins). One type may be better for some uses while the other may be more helpful in others. Know which is best to use in which circumstance and which one is active or turned off when encountering a database setup issue.
The EntityManager mechanism is activated and deactivated through the hibernate.hbm2ddl.auto property in src/test/resources/hibernate.properties. Set to create to turn on or comment out to turn off.
# src/test/resources/hibernate.properties ... #hibernate.hbm2ddl.auto=create
The maven plugin mechanism is turned activated by the sql-maven-plugin within the local pom.xml. You can either comment out the entire plugin, or comment out or mangle key sections of the plugin definition in order to turn off this capability.
# pom.xml <!-- runs schema against the DB <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>sql-maven-plugin</artifactId> ... </plugin> -->
The EntityManager technique is useful when making quick schema changes to your entities within Eclipse but not as good if you want to preserve data for analysis. The maven plugin technique is useful when you commonly build from maven after making entity changes. It is also useful in generating schema that can be analyzed after the build is complete.
Add the following to your .m2/settings.xml file. This will allow you to resolve the exercise archetype and set a default database for the exercise.
<profiles> <profile> <id>webdev-repositories</id> <repositories> <repository> <id>webdev</id> <name>ejava webdev repository</name> <url>http://webdev.jhuep.com/~jcs/maven2</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>webdev-snapshot</id> <name>ejava webdev snapshot repository</name> <url>http://webdev.jhuep.com/~jcs/maven2-snapshot</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> </snapshots> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>h2db</activeProfile> <!-- <activeProfile>h2srv</activeProfile> --> </activeProfiles>
Use the ejava.jpa:jpa-archetype to setup a new Maven project for this exercise. Activate the webdev-repositories profile (-Pwebdev-repositories) so that you can resolve the archetype off the Internet. The following should be run outside of the class example tree.
$ mvn archetype:generate -B -DarchetypeGroupId=info.ejava.examples.jpa -DarchetypeArtifactId=jpa-archetype -DarchetypeVersion=5.0.0-SNAPSHOT -DgroupId=myorg.entityex -DartifactId=entityEx -Pwebdev-repositories INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] [INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom --- [INFO] Generating project in Batch mode [WARNING] Archetype not found in any catalog. Falling back to central repository. [WARNING] Add a repsoitory with id 'archetype' in your settings.xml if archetype's repository is elsewhere. [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: jpa-archetype:5.0.0-SNAPSHOT [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: myorg.entityex [INFO] Parameter: artifactId, Value: entityEx [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: myorg.entityex [INFO] Parameter: packageInPathFormat, Value: myorg/entityex [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: myorg.entityex [INFO] Parameter: groupId, Value: myorg.entityex [INFO] Parameter: artifactId, Value: entityEx [INFO] Project created from Archetype in dir: .../proj/784/entityEx [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 20.490 s [INFO] Finished at: 2018-08-17T19:59:39-04:00 [INFO] Final Memory: 18M/317M [INFO] ------------------------------------------------------------------------
You should now have an instantiated template for a JPA project
entityEx/ ├── pom.xml └── src ├── main │ └── java │ └── myorg │ └── entityex │ └── Auto.java └── test ├── java │ └── myorg │ └── entityex │ └── AutoTest.java └── resources ├── hibernate.properties ├── log4j.xml └── META-INF └── persistence.xml
Verify the instantiated template builds in your environment
Activate the h2db profile (and deactivate the h2srv profile) to use an embedded file as your database. This option does not require a server but is harder to inspect database state in between tests. Remember that the "!" character must be escaped ("\!") for bash shells.
entityEx> mvn clean test -Ph2db -P\!h2srv ... -HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:/Users/jim/proj/784/entityEx/target/h2db/ejava] ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
Start your database server
$ java -jar M2_REPO/com/h2database/h2/1.4.197/h2-1.4.197.jar
Activate the h2srv profile (and deactivate the h2db profile) to use a running H2 database server. This option provides more interaction with your database but does require the server to be running.
entityEx> mvn clean test -P\!h2db -Ph2srv ... -HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:tcp://127.0.0.1:9092/./h2db/ejava] ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
You may now import the instantiated template into Eclipse as an "Existing Maven Project"