Using EclipseLink

"So how do I install and USE EclipseLink in my project

Since none of the previous answers really answer the question "How do I add the jpa impl. libraries to eclipse", here's the answer (took me an hour to figure it out myself!):

  • download a JPA implementation, for example eclipselink-2.5.1.v20130918-f2b9fc5.zip and extract the implementation libs. For example: eclipselink.jar jpa/*.jar to a directory

  • In eclipse open window -> preferences. In preferences window open build path -> user libraries

  • In user libraries click on new, type "name jpa" => ok

  • click on jpa and click "add external jars ... and select all extracted impl libraries

After that you can complete vogella's tutorial.

To the admins: You might want to flag the previous comments as invalid/offtopic. Especially you don't put the jars to the project path, as you are about to create a NEW project. However, the question is too broad, too.


I don't think you need to just learn how to use EclipseLink. EclipseLink is a reference implementation for JPA. There are a number of tutorials on the web that you can read up about regarding JPA. I have written a series of articles for beginners for building web applications and one of the sections deals with JPA (and EclipseLink). If you are interested take a look and I welcome discussion on it to improve it as well as my own understanding.

EDIT: Here's a more direct answer to your question. I hope it helps

There are a number of different download approaches to take for EclipeLink. This is one way:

Download EclipseLink Installer

Extract the eclipselink.jar from the download jlib directory and include it in your projects classpath.

Download the ejb3-persistence.jar and include it in your projects classpath.

I haven't got SQL server myself but you would need to include the sqljdbc jar in your classpath. Don't know what version you are using or if it even matters but you can try this link and download the 1033\sqljdbc_3.0.1301.101_enu.tar.gz file. Extract it and copy the sqljdbc.jar into your projects classpath.

Here's a simple standalone example:

persistence.xml (saved in your META-INF directory in your src folder)

<persistence 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_1_0.xsd"
    version="1.0">

    <persistence-unit name="escribs-pu" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
            <!-- haven't tested with SQL server so hope the below is correct -->
            <property name="eclipselink.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
            <property name="eclipselink.jdbc.url" value="jdbc:sqlserver://localhost;databaseName=MyDB" />
            <property name="eclipselink.jdbc.user" value="myusername" />
            <property name="eclipselink.jdbc.password" value="mypassword" />
        </properties>
    </persistence-unit>
</persistence>

Entity class:

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="person")
public class Person {
    private Long id;
    private String name;

    @Id
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}   

SimpleTest

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class SimpleTest {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("escribs-pu");

        Person person = new Person();
        person.setId(1L);
        person.setName("Clark");

        EntityManager em = null;
        EntityTransaction tx = null;
        try {
            em = emf.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            em.persist(person);
            tx.commit();

            System.out.println("Person id: " + person.getId());
        } catch (RuntimeException e) {
            tx.rollback();
            throw e;
        } finally {
            if (em != null && em.isOpen()) {
                em.close();
            }
        }
    }
}