Starting with JPA with JBoss and MySQL


  • JBoss 7.1.1
  • MySQL 5.5

Note: Make sure the datasource is configured properly. Visit the post ( to find out how to configure datasource.

Step 1: Configure persistence.xml

Create a file called persistence.xml and add the following content

<persistence xmlns=""
<persistence-unit name="testjpa" transaction-type="RESOURCE_LOCAL">
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="" value="update"/>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />

Make sure the datasource name matches the datasource configured in JBoss

Step 2: Create your bean

Create the bean which you would like to persist

package com.test;

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

@Entity (name="student")
public class StudentBean {
private int id;
private String name;
private int age;

public int getId() {
return id;
public void setId(int id) { = id;
public String getName() {
return name;
public void setName(String name) { = name;
public int getAge() {
return age;
public void setAge(int age) {
this.age = age;
  • @Entity annotations should be mentioned to denote the bean is a entity bean
  • @Id annotation makes the field as primary key of the table
  • @GeneratedValue annotation makes the field value as auto generated

Step 3: Persist your bean using EMF

EntityManagerFactory entityManagerFactory =  Persistence.createEntityManagerFactory("testjpa");
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction userTransaction = em.getTransaction();

StudentBean student = new StudentBean();
  1. testjpa is the name of persistence-unit in the persistence.xml
  2. Calling persist() just marks the bean for persistence and might not populate DB immediately, in order to see the data in database immediately flush() must be called.


  • The value for the property can be

                        validate: validate the schema, makes no changes to the database.
                        update: update the schema.
                        create: creates the schema, destroying previous data.
                        create-drop: drop the schema at the end of the session.

  •    In case there is no exception when bean is persisted but no row is found in DB then make sure the value for the property is not set to create-drop
  • Important:  The common mistake the developers make is to create the file in the META-INF folder under the project folder which is wrong.

The META-INF folder where persistence.xml is present should be in classpath but for JPA the root of the persistence unit is the WEB-INF/classes directory hence the persistence.xml should be copied in to WEB-INF/classes/META-INF folder (You might have to create META-INF folder manually).

If the persistence.xml is not created in the right folder the following error will be encountered

javax.persistence.PersistenceException: No Persistence provider for EntityManager named testjpa:       No META-INF/persistence.xml was found in classpath.

Wrong location for persistence.xml

  • In case you face the following exception make sure the persistence.xml has the  attribute transaction-type=”RESOURCE_LOCAL”

ERROR [stderr] (http-localhost- java.lang.NullPointerException
ERROR [stderr] (http-localhost-     at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(
ERROR [stderr] (http-localhost-     at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(
ERROR [stderr] (http-localhost-     at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(
ERROR [stderr] (http-localhost-     at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(
ERROR [stderr] (http-localhost-     at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(
ERROR [stderr] (http-localhost-     at org.hibernate.ejb.EntityManagerImpl.<init>(
ERROR [stderr] (http-localhost-     at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(
ERROR [stderr] (http-localhost-     at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(

17718 Total Views 6 Views Today
  • Nisar

    Good work… Apreciated…

  • Nisar


  • Mohammed Farooq

    HI am getting the following error when i deploy the project

    service jboss.jdbc-driver.mysql (missing)