JPA vs Hibernate in a nutshell

A fairly common question for developers approaching Object Relational Mapping (ORM) tools like Hibernate is what is the difference between Hibernate and JPA. In this article we are going to explain it in a nutshell. From an high level view, JPA is just like an interface without a concrete implementation of it. On the other … Read more

Debugging JPA in WildFly

In order to debug the Persistence Service in WildFly you can increase the org.jboss.as.jpa logging in order to gather the following information: INFO – will log information when persistence.xml has been parsed, when the persistence.xml has been deployed and when the persistence unit service has been stopped DEBUG – will inform you when Entity Managers … Read more

Persistence.xml cheatsheet

Need to setup a standalone persistence.xml for your application? then the following cheatsheet could help you! <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_1_0.xsd” version=”1.0″> <!– hsqldb –> <persistence-unit name=”hsqldb”> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name=”hibernate.archive.autodetection” value=”class” /> <property name=”hibernate.dialect” value=”com.mysema.query.jpa.support.ExtendedHSQLDialect” /> <property name=”hibernate.connection.driver_class” value=”org.hsqldb.jdbcDriver” /> <property name=”hibernate.connection.url” value=”jdbc:hsqldb:file:target/testdb;shutdown=true” /> <property name=”hibernate.connection.user” value=”sa” /> <!– <property name=”hibernate.show_sql” value=”true”/> –> <property … Read more

How to use JPA from a JBoss Web application ?

In simple terms, if you are not using EJB or CDI, the user is expected to interact with javax.transaction.UserTransaction which defines the transaction boundaries. Here is an example Servlet which persists an Entity by injecting the UserTransaction and adding the EntityManager via the @PersistenceContext annotation: @WebServlet(name=”JPAServlet”, urlPatterns={“/jpademo”}) public class JPAServlet extends HttpServlet { private static … Read more

How to deploy the Persistence unit in the global JNDI ?

By default the persistence unit are available in the java: Context. If you wish to make them available also in the global naming Context you have to add two properties to your persistence.xml configuration file: <persistence>    <persistence-unit name=”manager1″>       <jta-data-source>java:/MySQLDS</jta-data-source>       <properties>          <property name=”jboss.entity.manager.jndi.name” value=”java:/Manager1″/>          <property name=”jboss.entity.manager.factory.jndi.name” value=”java:/Manager1Factory”/>       </properties>    </persistence-unit> … Read more

How to use multiple database in persistence.xml?

The Java Persistence API allows you to define multiple persistence units, each of which can map to a separate database. If you look at the persistence.xml file : <persistence> <persistence-unit name=”sample-db1″> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>jdbc/SamplesDB</jta-data-source> </persistence-unit> <persistence-unit name=”sample-db2″> <provider> oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider </provider> <jta-data-source>jdbc/SamplesDB2</jta-data-source> </persistence-unit> </persistence> The sample-db1 and sample-db2 persistence units are used to create and inject the … Read more

How to use an env variable in your persistence.xml ?

In this short tutorial we will show how to use variables in your persistence.xml so that you can dynamically specify the data source which is used by our application.  In WildFly the simples and most elegant way to do that is enabling the property replacement in the ee subsystem, as in the following example: <subsystem … Read more