Magazine
 
Maven2 with JPA Example

Getting Started with Maven2 :

Download Maven2 from maven.apache.org, unzip
the archive into your local directory. Here we are assuming that the local repository already exists.
Set the JAVA_HOME variable to point to the JDK installation directory and MAVEN_HOME to point to the Maven directory and add the MAVEN_HOME/bin to the PATH environment variable. To test, whether the path has been set properly. Type mvn -version on the command prompt.

C:\>mvn -versionMaven
version: 2.0.7Java version:1.5.0OS name: “windows 2000” version: “5.0” arch:“x86”

Developing java presistence
example with Maven2


Here I will show, how to build a very simple example that uses java persistence API. Maven2 highly simplifies the build process, all the required libraries are downloaded automatically only when it really requires.


 

Directory Structure of the example: Our application must maintain a directory structure understandable by the maven project build tool. It is shown below:

Our Application includes: Our application includes the source folder "src" and an pom.xml file. Source folder contains java files, here HelloWorld.java and Message.java files are contained in the hello folder. Resource folder exists parallel to the java folder inside the main folder and contains log4j.properties file and the META-INF folder. META-INF folder contains persistence.xml file. "pom.xml" is the main file for the maven build system that includes information about the tools required to build the application.



1. Create a java class (lets take it Message.java ), where you map java class/ properties to database table/fields. Java persistence annotations will do this job for us:

package hello;
import javax.persistence.*;
@Entity
@Table(name = "MESSAGES")
public class Message {
@Id @GeneratedValue @Column(name = "MESSAGE_ID")
private Long id;

Download Maven2JPA Source Code

Sept 2007 | Java Jazz Up | 35
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,   Download PDF