Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Persistence.xml

 
Khaled Mahmoud
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to EJB and preparing for the Sun Certified Business Component Developer.
I am now reading the book EJB 3.0 for Oreilly.

How many persistence.xml and application can have?

In the chapter about EntityManger section 5.3 the book says one of the places that a persistence.xml file located is at :

A plain JAR file within the classpath of a regular Java SE program

Does this mean that I can deploy many persistence units in java application each with its own persistence.xml file in a seperete jar file.
 
Andor Nemeth
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Khaled Mahmoud:
I am new to EJB and preparing for the Sun Certified Business Component Developer.
I am now reading the book EJB 3.0 for Oreilly.

How many persistence.xml and application can have?

In the chapter about EntityManger section 5.3 the book says one of the places that a persistence.xml file located is at :

A plain JAR file within the classpath of a regular Java SE program

Does this mean that I can deploy many persistence units in java application each with its own persistence.xml file in a seperete jar file.


Yes, of course it works.
I've just tried it. I've created an entity bean and packed it into a JAR file with a persistence.xml (with <persistence-unit name="domain2"> ) and i've deployed it into my EJB 3.0 container (JBoss).

Then, i've created an other JAR with an other persistence.xml(with <persistence-unit name="domain1"> ) and i've put an other entity bean with a stateless session bean into it. The stateless bean had some methods to persist and read out my entity beans from "both of the JARs" (corretly said: persistence units).

Of course, i've injected (dependency injection) TWO EntityManager references (respectively for both persistence units) into the stateless session bean like this:

@PersistenceContext (unitName="domain1")
private EntityManager em1;

@PersistenceContext (unitName="domain2")
private EntityManager em2;

persist() and find() have worked for both of the persistence units.

I hope I've helped you.
[ July 12, 2007: Message edited by: Andor Nemeth ]
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also have more than 1 persistence unit defined in a single persistence.xml.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic