0

Is it possible to run entity bean without container? If so example please.

laksys
  • 3,228
  • 4
  • 27
  • 38

2 Answers2

3

JPA can be used outside of a Java EE container, yes. The specification of JPA2 starts with these words:

This document is the specification of the Java API for the management of persistence and object/relational mapping with Java EE and Java SE.

(emphasis mine)

You won't have automatic support for persistence unit injection, JTA support, etc, though. Spring helps in this area.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks @JB Nizet the following post helps me lot. http://stackoverflow.com/questions/9419223/can-jpa-2-0-be-used-without-javaee-6?rq=1 – laksys Dec 01 '12 at 16:22
0

The main differences with running inside container are definition of persistence-unit and handling transactions. For an example, persistence-unit is defined as follows, in persistence.xml:

<persistence-unit name="pu_name" transaction-type="RESOURCE_LOCAL">

Then you will need to obtain EntityTransaction and use it:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu_name");
EntityManager em = emf.createEntityManager();

EntityTransaction tx = em.getTransaction();
tx.begin();
...
...
tx.commit();

em.close();
emf.close();
turgayze
  • 166
  • 1
  • 5