Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
225 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...