Standalone JPA Project Using Java SE: A Maven Way
March 7, 2014 Leave a comment
In this post, I will show how to create a simple JPA application by using maven and Java SE JDK. All the other libraries will be taken care of by the remote repository in maven, which includes eclipselink and embedded derby db.
I will be focusing on how to use maven to generate, what project structure and programs created, how to execute the application, how to verify the database objects, and what targets can be deployed.
I hope this will help you get started, especially without using any IDEs like Eclipse, Netbean etc.
I won’t go to explain what JPA is. There are many resources and books available.
1. Generate JPA App in Seconds
Make sure to enter ’46’ as artifactID (46 is for com.github.lalyos:standalone-jpa-eclipselink-archetype) when asked in the following:
mvn archetype:generate -DgroupId=henry416 -DartifactId=test1 ................. Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 354: 46 Choose com.github.lalyos:standalone-jpa-eclipselink-archetype version: 1: 0.0.1 2: 0.0.2 Choose a number: 2: 2 [INFO] Using property: groupId = henry416 [INFO] Using property: artifactId = test1 Define value for property 'version': 1.0-SNAPSHOT: : [INFO] Using property: package = henry416 Confirm properties configuration: groupId: henry416 artifactId: test1 version: 1.0-SNAPSHOT package: henry416 Y: : [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: standalone-jpa-eclipselink-archetype:0.0.2 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: henry416 [INFO] Parameter: artifactId, Value: test1 [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: henry416 [INFO] Parameter: packageInPathFormat, Value: henry416 [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: henry416 [INFO] Parameter: groupId, Value: henry416 [INFO] Parameter: artifactId, Value: test1 [INFO] project created from Archetype in dir: /home/henry/Test/jpatest/test1 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1:42.092s [INFO] Finished at: Thu Mar 06 21:10:38 EST 2014 [INFO] Final Memory: 10M/25M [INFO] ------------------------------------------------------------------------
2. Project Structures Generated
henry@brimley:~/Test/jpatest/test1$ ls -ld $(find .) drwxr-xr-x 3 henry henry 4096 2014-03-06 22:37 . -rw-r--r-- 1 henry henry 70 2014-03-06 21:10 ./ij.properties -rw-r--r-- 1 henry henry 1185 2014-03-06 21:10 ./pom.xml -rwx------ 1 henry henry 60 2014-03-06 21:10 ./run.sh -rwx------ 1 henry henry 107 2014-03-06 21:10 ./show-derby.sh drwxr-xr-x 3 henry henry 4096 2014-03-06 21:10 ./src drwxr-xr-x 4 henry henry 4096 2014-03-06 21:10 ./src/main drwxr-xr-x 3 henry henry 4096 2014-03-06 21:10 ./src/main/java drwxr-xr-x 4 henry henry 4096 2014-03-06 21:10 ./src/main/java/henry416 drwxr-xr-x 2 henry henry 4096 2014-03-06 21:10 ./src/main/java/henry416/domain -rw-r--r-- 1 henry henry 928 2014-03-06 21:10 ./src/main/java/henry416/domain/Department.java -rw-r--r-- 1 henry henry 992 2014-03-06 21:10 ./src/main/java/henry416/domain/Employee.java drwxr-xr-x 2 henry henry 4096 2014-03-06 21:10 ./src/main/java/henry416/jpa -rw-r--r-- 1 henry henry 1597 2014-03-06 21:10 ./src/main/java/henry416/jpa/JpaTest.java drwxr-xr-x 3 henry henry 4096 2014-03-06 21:10 ./src/main/resources drwxr-xr-x 2 henry henry 4096 2014-03-06 21:10 ./src/main/resources/META-INF -rw-r--r-- 1 henry henry 1205 2014-03-06 21:10 ./src/main/resources/META-INF/persistence.xml
The source codes you may be interested in the following separate links:
./pom.xml
./src/main/resources/META-INF/persistence.xml
./src/main/java/henry416/domain/Department.java
./src/main/java/henry416/domain/Employee.java
./src/main/java/henry416/jpa/JpaTest.java
3. Test Run
henry@brimley:~/Test/jpatest/test1$ cat run.sh mvn compile exec:java -Dexec.mainClass=henry416.jpa.JpaTest henry@brimley:~/Test/jpatest/test1$ chmod 700 *.sh henry@brimley:~/Test/jpatest/test1$ ./run.sh [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building test1 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test1 --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ test1 --- [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 3 source files to /home/henry/Test/jpatest/test1/target/classes [INFO] [INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ test1 >>> [INFO] [INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ test1 <<< [INFO] [INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ test1 --- num of employess:2 next employee: Employee [id=3, name=Captain Nemo, department=java] next employee: Employee [id=2, name=Jakab Gipsz, department=java] .. done [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 27.590s [INFO] Finished at: Thu Mar 06 21:15:27 EST 2014 [INFO] Final Memory: 24M/61M [INFO] ------------------------------------------------------------------------
4. Result
henry@brimley:~/Test/jpatest/test1$ cat show-derby.sh mvn dependency:copy-dependencies java -cp 'target/dependency/*' org.apache.derby.tools.ij -p ij.properties henry@brimley:~/Test/jpatest/test1$ ./show-derby.sh [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building test1 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.8:copy-dependencies (default-cli) @ test1 --- [INFO] Copying eclipselink-2.2.1.jar to /home/henry/Test/jpatest/test1/target/dependency/eclipselink-2.2.1.jar [INFO] Copying derbytools-10.8.2.2.jar to /home/henry/Test/jpatest/test1/target/dependency/derbytools-10.8.2.2.jar [INFO] Copying derby-10.8.2.2.jar to /home/henry/Test/jpatest/test1/target/dependency/derby-10.8.2.2.jar [INFO] Copying javax.persistence-2.0.3.jar to /home/henry/Test/jpatest/test1/target/dependency/javax.persistence-2.0.3.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.026s [INFO] Finished at: Thu Mar 06 21:17:51 EST 2014 [INFO] Final Memory: 8M/21M [INFO] ------------------------------------------------------------------------ ij version 10.8 CONNECTION0* - jdbc:derby:simpleDb * = current connection ij> show schemas; TABLE_SCHEM ------------------------------ APP NULLID SQLJ SYS SYSCAT SYSCS_DIAG SYSCS_UTIL SYSFUN SYSIBM SYSPROC SYSSTAT TEST1 12 rows selected ij> select * from TEST1.DEPARTMENT; ID |NAME ----------------------------------------- 1 |java 1 row selected ij> select * from TEST1.EMPLOYEE; ID |NAME |DEPARTMENT_ID -------------------------------------------------------------- 3 |Captain Nemo |1 2 |Jakab Gipsz |1 2 rows selected ij> exit;
5. Deployment Target
henry@brimley:~/Test/jpatest/test1/target$ ls -ld $(find .) drwxr-xr-x 4 henry henry 4096 2014-03-06 21:17 . drwxr-xr-x 4 henry henry 4096 2014-03-06 21:15 ./classes drwxr-xr-x 4 henry henry 4096 2014-03-06 21:15 ./classes/henry416 drwxr-xr-x 2 henry henry 4096 2014-03-06 21:15 ./classes/henry416/domain -rw-r--r-- 1 henry henry 1691 2014-03-06 21:15 ./classes/henry416/domain/Department.class -rw-r--r-- 1 henry henry 1888 2014-03-06 21:15 ./classes/henry416/domain/Employee.class drwxr-xr-x 2 henry henry 4096 2014-03-06 21:15 ./classes/henry416/jpa -rw-r--r-- 1 henry henry 3080 2014-03-06 21:15 ./classes/henry416/jpa/JpaTest.class drwxr-xr-x 2 henry henry 4096 2014-03-06 21:15 ./classes/META-INF -rw-r--r-- 1 henry henry 1205 2014-03-06 21:15 ./classes/META-INF/persistence.xml drwxr-xr-x 2 henry henry 4096 2014-03-06 21:17 ./dependency -rw-r--r-- 1 henry henry 2671577 2014-03-06 21:17 ./dependency/derby-10.8.2.2.jar -rw-r--r-- 1 henry henry 174969 2014-03-06 21:17 ./dependency/derbytools-10.8.2.2.jar -rw-r--r-- 1 henry henry 6412045 2014-03-06 21:17 ./dependency/eclipselink-2.2.1.jar -rw-r--r-- 1 henry henry 126856 2014-03-06 21:17 ./dependency/javax.persistence-2.0.3.jar
6. Run As Java Application
henry@brimley:~/Test/jpatest/test1$ cd target/classes henry@brimley:~/Test/jpatest/test1/target/classes$ java -cp '../dependency/*' henry416.jpa.JpaTest num of employess:2 next employee: Employee [id=3, name=Captain Nemo, department=java] next employee: Employee [id=2, name=Jakab Gipsz, department=java] .. done
7. Summary
By using maven, it’s pretty easy to create a project structure for a JPA application (Kudos to you Lajos Papp). By replacing those POJO entity classes with your own classes, modifying persistence.xml to your local database and pom.xml to the latest maven repository, you can code a real world java application in JPA. I hope this will make JPA programming more interesting.