Nowbody calls me Ignacio. I’m plunchete, man!

Using OpenJPA

Posted in OpenJPA by plunchete on May 6, 2007

I’ve implemented a simple example using OpenJPA as persistence system. This is my first experience using OpenJPA and I like it, I think that OpenJPA is really a great API. I hope that this post serves as aid to other people who start with OpenJPA.

I started using the “hellojpa” example and the OpenJPA User’s Guide

Writting a simple persistence class:

public class Person {
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id_person")
	private long idPerson;

	@Column(nullable = false, length = 30)
	private String name;

	@Column(name = "e_mail")
	private String mail;

	public Person() {}

	public String getMail() {
		return mail;

	public void setMail(String mail) {
		this.mail = mail;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public long getIdPerson() {
		return idPerson;

	public void setIdPerson(long idPerson) {
		this.idPerson = idPerson;

Annotations are very intuitive and powerful.

Writting the persistence.xml I used MySQL as database.

<persistence xmlns=""
    <persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
            <property name="openjpa.jdbc.SynchronizeMappings"
            <property name="openjpa.ConnectionURL"
            <property name="openjpa.ConnectionDriverName"
            <property name="openjpa.ConnectionUserName"
            <property name="openjpa.ConnectionPassword"

The property “openjpa.jdbc.SynchronizeMappings” is amazing, you don’t need any sql definitions of the tables or the rows, the OpenJPA creates the tables with your object information and if you add a variable it makes an alter automatically.

And finally contructing the EntityManage and saving the object:

        EntityManagerFactory factory = Persistence.
                createEntityManagerFactory("example", System.getProperties());

        EntityManager em = factory.createEntityManager();


        Person person = new Person();
        person.setName("Ignacio Andreu");
        person.setMail("plunchete AT gmail DOT com");



And this is all 🙂


11 Responses

Subscribe to comments with RSS.

  1. […] with JPA Continuing with the JPA Basic Example I’ve written another example with relations between […]

  2. Rajesh said, on January 7, 2008 at 8:26 pm

    How do i compile and run this openjpa code ?

  3. mohammad said, on October 17, 2008 at 7:32 pm

    can you tell me how to find dirty fields before being flushed using LifecycleListenerS? I am going to build an auditing system in my application. Thanks

  4. plunchete said, on October 21, 2008 at 11:32 am

    Hi Mohammad, have you tried to send and email to the OpenJPA list?


  5. Robin said, on October 20, 2009 at 4:41 am

    Great to learn about the property “openjpa.jdbc.SynchronizeMappings”. Thanks!

  6. Keith said, on December 4, 2009 at 3:57 pm

    I have been trying to get an openJPA, WAS7, EJB3 environent working for the past few days now, but have hit a brickwall with ‘inserting’ data.

    I have two tables and using the
    @Table(name = “tbl_user_details”)
    @SecondaryTable(name = “tbl_secure_details”, pkJoinColumns = @PrimaryKeyJoinColumn(name = “id”))

    But all that happens at after em.persist(dao);
    is, the user details table has a new row created but with NULL values (nothing happens to the secure table.

  7. Keith said, on December 4, 2009 at 5:02 pm


    Something else I noticed, I have this in two persistence-unit as I have two databases for my app, and for some reason it has created all of the tables in database1 in database2!

  8. yared said, on February 13, 2010 at 1:58 am

    all the manuals i have seen today said the same, parts of code. but what I have to do to make this work, servers, libraries, and all that things.

    sorry, mi english is really bad, son in spanish:

    Todos los manuales que he visto hoy dicen lo mismo, tienen solo partes de codigo que ya se que hay que usar pero no me dicen que es lo que hay que instalar ni como, asumen que ya somos expertos, nadie considera a los pobres pavos.

  9. Werner said, on March 16, 2010 at 2:13 pm

    Hey guys!

    Do you know (or have an opinion on) whether it makes sense to use OpenJPA for a veeeryy light-weight swing-only client???

    Thanks a lot!

  10. plunchete said, on March 16, 2010 at 3:46 pm

    Hi Werner,

    IMHO frameworks like OpenJPA or Hibernate are not very light-weight. Depens on your project but I really enjoy using frameworks like siena. Siena is light-weight and simple 🙂


  11. Andrew Belt said, on July 29, 2010 at 7:50 pm

    Wow! This API is amazing! Thanks for the advise on SynchronizeMappings.

    And plunchete, I’ll check out Siena. It look promising.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: