This tutorial will show to build a JPA Project manually without any JPA Eclipse plugin.
1. Create a Java Project.
Your will see the project structure as below image
2. Create "META-INF" directory in "src" directory. Right Click "src" => New => Folder. Make sure Folder name with "META-INF". Click "Finish" button.
3. Create "persistence.xml" file in "META-INF" directory. Right Click "META-INF" => New => File. Put the file name like "persistence.xml". Click "Finish" button.
4. Copy the following content into "persistence.xml" file. The content is for only EclipseLink Persistence Provider and MySQL Database.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="METADATA_JPA" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/metadatajpa"/>
<property name="javax.persistence.jdbc.user" value="metadatajpa"/>
<property name="javax.persistence.jdbc.password" value="metadatajpa"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
|
5. Create library directory for the project. Right Click the project => New => Folder. Name the folder name as "lib". Click "Finish" button.
6. Put the following jar files into "lib" directory.
- javax.persistence_2.x.x.jar
- eclipselink-2.5.0.jar
- mysql-connector-java-5.1.6-bin.jar
7. Set the jar files into the "Class-Path". At Step-6, it is just put the jar files into "lib" directory. It is not in the "Class-Path". Right Click the project => Properties => Java Build Path => Libraries Tab => Click "Add JARs" button.
8. Select the all of jar files form "lib" directory. Click "OK" button.
9. Create an entity class. Right Click "src" => New => Class.
For example : Class Name => Person, Package Name => org.metadata.jee.jpa.entities.
Put some of the fields and getter/setter for Person Entity.
package
org.metadata.jee.jpa.entities;
import
javax.persistence.Entity;
import
javax.persistence.Id;
@Entity
public class Person {
@Id
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String
getName() {
return name;
}
public void
setName(String name) {
this.name = name;
}
}
|
10. Configure Person Entity in persistence.xml file as below.
<?xml version="1.0"
encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="METADATA_JPA"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.metadata.jee.jpa.entities.Person</class>
<properties>
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/metadatajpa"/>
<property name="javax.persistence.jdbc.user"
value="metadatajpa"/>
<property name="javax.persistence.jdbc.password"
value="metadatajpa"/>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
|
11. It is finished for environment setup of JPA project. Complete folder structure as below image.
No comments:
Post a Comment