HelloWorld with EclipseLink and MySQL

This is a simple example of standalone java application using Java Persistence API (JPA), EclipseLink, and MySql. It consists of an entity class, a main class, and a persistence.xml file. No need to create tables, as they are created and dropped automatically.

META-INF/persistence.xml must be at the root of the persistence unit. In my example project, it's located at $HOME/NetBeansProjects/greeting/build/classes/META-INF/persistence.xml.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="greetingPU" transaction-type="RESOURCE_LOCAL">
<property name="eclipselink.jdbc.password" value=""/>
<property name="eclipselink.jdbc.user" value="root"/>
<property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="INFO"/>
Entity class (generated by NetBeans 6.1):
package javahowto;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Id;

public class Greeting implements Serializable {
private static final long serialVersionUID = 1L;

private Long id;

private String message;

public Long getId() {
return id;

public void setId(Long id) {
this.id = id;

public String getMessage() {
return message;

public void setMessage(String message) {
this.message = message;

public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;

public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Greeting)) {
return false;
Greeting other = (Greeting) object;
if ((this.id == null && other.id != null) ||
(this.id != null && !this.id.equals(other.id))) {
return false;
return true;

public String toString() {
return "javahowto.Greeting[id=" + id + ", message='" + getMessage() +
Main class:
package javahowto;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Main {
private static final Logger logger = Logger.getLogger("javahowto.greeting");
private static final String PERSISTENCE_UNIT_NAME = "greetingPU";
private EntityManagerFactory emf;
private EntityManager em;

public static void main(String[] args) {
Main main = new Main();

private void createAndRead() {
Greeting g = new Greeting();
g.setMessage("hello, createAndRead");

//g should be written to database now.
//Read it from db (no transaction context needed for em.find method)
Greeting g2 = em.find(Greeting.class, g.getId());
logger.info("Greeting " + g.getId() + " from db: " + g2);

private void createAndRollback() {
Greeting g = new Greeting();
g.setMessage("hello, createAndRollback");

logger.info("Persisted " + g + ", but the transaction was rolled back.");
Greeting g2 = em.find(Greeting.class, g.getId());
logger.info("Greeting " + g.getId() + " from db: " + g2); //should be null

private void initEntityManager() {
emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
em = emf.createEntityManager();
To compile the project,
/NetBeansProjects/greeting/src >
javac -d ../build/classes -cp $GLASSFISH_HOME/modules/javax.persistence.jar javahowto/*.java
Start MySql on localhost, and run the following command. No need to create the table since our persistence.xml tells EclipseLink to automatically drop and create it.
/home/javahowto/mysql > bin/mysqld_safe &

/NetBeansProjects/greeting/build/classes >
java -cp $HOME/tmp/mysql-connector-java-5.1.5-bin.jar:$GLASSFISH_HOME/modules/javax.persistence.jar:$GLASSFISH_HOME/modules/org.eclipse.persistence.jpa.jar:$GLASSFISH_HOME/modules/org.eclipse.persistence.core.jar:. javahowto.Main

[EL Info]: 2008.10.20 11:56:24.712--ServerSession(4744654)--EclipseLink, version: Eclipse Persistence Services - 1.0.1 (Build 20080905)
[EL Info]: 2008.10.20 11:56:25.163--ServerSession(4744654)--file:/home/javahowto/NetBeansProjects/greeting/build/classes/-greetingPU login successful
Oct 20, 2008 11:56:25 AM javahowto.Main createAndRead
INFO: Greeting 1 from db: javahowto.Greeting[id=1, message='hello, createAndRead']
Oct 20, 2008 11:56:25 AM javahowto.Main createAndRollback
INFO: Persisted javahowto.Greeting[id=2, message='hello, createAndRollback'], but the transaction was rolled back.
Oct 20, 2008 11:56:25 AM javahowto.Main createAndRollback
INFO: Greeting 2 from db: null
To stop MySQL server:
/home/javahowto/mysql > bin/mysqladmin -u root shutdown


Anonymous said...

Thank! It is very useful!

Anonymous said...

Thanks, what a nice & quick HelloWorld!

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Anonymous said...

Very useful post!

Vũ Diệu Linh said...

I enjoyed on reading your blog post. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. Please visit my website, Friv 4000 Games is where all the free friv games.
Friv 4000