Preloader image

The JPA provides Java developers with an object/relational mapping facility for managing relational data in Java applications.

@Entity define the entity class that you want to convert into a table in a database. @Id define the main key of the table. @GeneratedValue provides for the specification of generation strategies for the values of primary keys.


package org.superbiz.injection.jpa;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

public class Movie {
    private long id;
    private String director;
    private String title;
    private int year;

    public Movie() {
        // no-op

    public Movie(String director, String title, int year) {
        this.director = director;
        this.title = title;
        this.year = year;

    public long getId() {
        return id;

    public String getDirector() {
        return director;

    public void setDirector(String director) {
        this.director = director;

    public String getTitle() {
        return title;

    public void setTitle(String title) {
        this.title = title;

    public int getYear() {
        return year;

    public void setYear(int year) {
        this.year = year;


 <persistence-fragment version="2.0">
   <persistence-unit-fragment name="movie-unit">


package org.superbiz.injection.jpa;

import org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory;
import org.junit.Test;

import javax.ejb.embeddable.EJBContainer;
import javax.naming.Context;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import java.util.Properties;

import static org.junit.Assert.assertTrue;

public class MoviesTest {
    private EntityManagerFactory emf;

    public void test() throws Exception {
        final Properties p = new Properties();
        p.put("movieDatabase", "new://Resource?type=DataSource");
        p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
        p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");

        final EJBContainer container = EJBContainer.createEJBContainer(p);
        final Context context = container.getContext();
        context.bind("inject", this);

        assertTrue(((ReloadableEntityManagerFactory) emf).getManagedClasses().contains(Movie.class.getName()));



 <persistence version="2.0"
   <persistence-unit name="movie-unit">
       <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>