Persisting An Annotation-Less POJO With JPA

To persist an annotation-less POJO with JPA:

public class Workshop {

    private long id;
    private String name;

    public Workshop() {

    public Workshop(long id, String name) { = id; = name;

you will have to provide the lacking metadata in a XML descriptor instead of annotations:

<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns=""
                 xsi:schemaLocation=" orm_2_0.xsd"
    <entity class="com.airhacks.jpa.orm.Workshop">
        <table name="T_WORKSHOP"/>
            <id name="id">
                <generated-value strategy="AUTO"/>
            <basic name="name">
                <column name="w_name" length="100"/>

The persistence.xml file has to point to the location of the orm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns=""
    <persistence-unit name="it" transaction-type="RESOURCE_LOCAL">


            <property name="javax.persistence.jdbc.url"
            <property name="javax.persistence.jdbc.driver"
            <property name="javax.persistence.schema-generation.database.action"

Now an annotation-less POJO can be stored using JPA:

import com.airhacks.rulz.em.EntityManagerProvider;
import org.junit.Rule;
import org.junit.Test;

public class WorkshopIT {

    public EntityManagerProvider provider = EntityManagerProvider.persistenceUnit("it");

    public void crud() {
        provider.em().merge(new Workshop());


The idea for this post came from 16th Q&A. Big thanks to Eddy Young for asking this question.

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting


Why? For god sake, why? Ten years ago maybe, but now?

Posted by Jirka Pinkas on July 10, 2015 at 08:55 AM CEST #

Reminds me of Hibernate hbm mapping files. Never again!

Posted by javaservant on July 10, 2015 at 11:06 PM CEST #

Just want to know whether the input
<mapping-file>META-INF/orm.xml</mapping-file> is obligaroty in the persistence.xml-File? I was thinking that, it will be always picks up, when it's present.

Posted by Gilles on July 20, 2015 at 03:57 PM CEST #

For those asking "why": because you don't always control the POJO implementation?

Posted by Thom on August 26, 2015 at 04:51 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments