I know that with the instruction:
Persistence.createEntityManagerFactory("persistence-unit-name");
The JPA persistence mechanism reads the "persistence.xml" file, looks for the persistence unit called "persistence-unit-name", and constructs the EntityManagerFactory based on it.
My question is, how can I force JPA to take a file different from "persistence.xml"?? for example, "persistence-test.xml".
There is no standardized JPA way to do this, although individual JPA providers may provide a way. I would suggest the standard way to have a different class path for main and test.
For example, if you use Maven, and you have two META-INF/persistence.xml
files, one in src/main/resources
and one in src/test/resources
, tests will pick up the one in src/test/resources
, because Maven puts test classes / resources ahead of main classes / resources in the classpath. You can easily configure Ant to work in similar ways.
If you need more advanced logic, consider using Spring's JPA support. It will let you deal with advanced situations like integrating multiple persistence.xml files.