Friday, April 13, 2012

Widgets

Starting with JPA with JBoss and MySQL

Environment

  • JBoss 7.1.1

  • MySQL 5.5


Note: Make sure the datasource is configured properly. Visit the post (http://vkslabs.com/adding-mysql-data-store-to-jboss-7-x/) to find out how to configure datasource.

Step 1: Configure persistence.xml

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

[xml]
<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_2_0.xsd"
version="2.0">
<persistence-unit name="testjpa" transaction-type="RESOURCE_LOCAL">
<jta-data-source>java:jboss/datasources/myDS</jta-data-source>
<class>com.test.StudentBean</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
</properties>
</persistence-unit>
</persistence>
[/xml]

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

[java]
package com.test;

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

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

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
[/java]

  • @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

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

userTransaction.begin();
StudentBean student = new StudentBean();
student.setName(name);
student.setAge(age);
em.persist(student);
em.flush();
userTransaction.commit();
em.close();
entityManagerFactory.close();
[/java]

  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.


Note:

  • The value for the property hibernate.hbm2ddl.auto 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 hibernate.hbm2ddl.auto 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-127.0.0.1-8080-1) java.lang.NullPointerException
ERROR [stderr] (http-localhost-127.0.0.1-8080-1)     at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73)
ERROR [stderr] (http-localhost-127.0.0.1-8080-1)     at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:115)
ERROR [stderr] (http-localhost-127.0.0.1-8080-1)     at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
ERROR [stderr] (http-localhost-127.0.0.1-8080-1)     at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1207)
ERROR [stderr] (http-localhost-127.0.0.1-8080-1)     at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:176)
ERROR [stderr] (http-localhost-127.0.0.1-8080-1)     at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:89)
ERROR [stderr] (http-localhost-127.0.0.1-8080-1)     at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:125)
ERROR [stderr] (http-localhost-127.0.0.1-8080-1)     at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:120)

6 comments:

  1. Good work... Apreciated...

    ReplyDelete
  2. HI am getting the following error when i deploy the project

    service jboss.jdbc-driver.mysql (missing)

    ReplyDelete
  3. payday loans

    This can not be the same if you've found yourself a long lasting home loan

    ReplyDelete
  4. netticasinot

    Starting with JPA with JBoss and MySQL | VKS Labs

    ReplyDelete
  5. pikalaina

    Starting with JPA with JBoss and MySQL | VKS Labs

    ReplyDelete