Skip to main content

How simple can persistence.xml get? Why do I need it at all?

1 reply [Last post]
cayhorstmann
Offline
Joined: 2003-06-13

I have a simple app with a single persistence unit.

I just found by trial and error that I can greatly simplify persistence.xml (in b36).

<?xml version="1.0" encoding="UTF-8"?>

I don't have to give a name to the persistence context. I simply inject with

@PersistenceContext EntityManager em;

But the name attribute is required in persistence-unit. In fact, section 6.2 of the spec states "A persistence unit must have a name." Apparently, the name of length 0 is ok.

The app server has a default data source (jdbc/__default), and mercifully I no longer need to specify it.

Is this extreme simplification going to get me in trouble?

If not, why do I need to bother with persistence.xml at all?

Cay

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mperezma
Offline
Joined: 2005-03-22

Hi Cay,

I readed somewhere that if you only have one persistence unit defined, then you don't need to specify its name in the injection tag.

I think the descriptor is mainly useful in j2se environments, you make the declaration of persistent classes and db connection properties in this xml. You don't need any app server in order to only use Persistence API.

In fact, I'm currently using the API from inside Application Server 4.2 this way: I put the descriptor inside appserv-rt.jar, I put persistence library with jdbc driver jar in appserver/lib folder, and start the instance with -javaagent option linking to toplink.

I must restart the server only when my persistence descriptor changes (in practice when new classes are added to the unit) but persistence unit can be developed and tested separately in j2se prior to use it in the server side. Then I can make experiments with EA releases of Persistence implementation with the stability of 4.2 AS release.

mariO