9

This is my stateless bean:

@Stateless
public class Finder {
  @PersistenceContext(unitName = "production")
  EntityManager em;
  [...]
}

It explicitly defines that the name of persistence unit is production. This unit is configured in persistence.xml, and everything is fine. When I'm unit testing this class I have to use another persistence unit, with different set of properties and configuration settings. How should I organize it? Create another <persistence-unit> element in persistence.xml? Does any best practice exist for this?

yegor256
  • 102,010
  • 123
  • 446
  • 597

4 Answers4

9

I use the same persistent unit name but different persistence.xml files (how are you going to automate testing if you need to edit the code to enable the "test mode"?).

If you're still using Maven, Maven supports natively having testing versions of configuration files:

  • the "production" persistence.xml goes under src/main/resources/META-INF
  • the "test" persistence.xml goes under src/test/resources/META-INF and is used for testing.
David Mann
  • 1,874
  • 2
  • 16
  • 19
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 1
    Thanks, this is what I was looking for (test copy of `persistence.xml`). Of course I'm using Maven, what else? :) – yegor256 Oct 05 '10 at 09:56
  • @Vincenzo Exactly, what else? :) – Pascal Thivent Oct 05 '10 at 10:13
  • @PascalThivent Pascal I highly regard all your SO answers. Can you tell me why you use a separate persistence.xml with a PU that has the same name? I realize its convenient this way if Maven can choose the right PU to use. However my projects are not mavenized, so I have to jump through a few more hoops to use two persistence.xml files. Since two persistence.xml files leads to duplication of configuration, what other benefits come from using two different persistence.xml files? – David Mann Nov 19 '13 at 18:17
2

I simply created another <persistence-unit> element in persistence.xml.

willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
0

I had a similar problem and I want to provide another approach. I wanted to run test an prod, but I don't use two persistence.xml or code modifications. I just have one persistence unit, but different runtime environments (standalone.xml, Wildfly). Let's say that I want run against my development database, I start Wildfly with the test runtime environment. When I want simulate it as real user I run against the prod environment. The only difference is the datasource entry in standalone.xml. The descriptor is always the same (e.g. myappDS, so the persistence unit declaration in persistence.xml is always the same too), but in test server the datasource entry points to my test database, in prod the datasource entry points to my prod database.

Bevor
  • 8,396
  • 15
  • 77
  • 141
0

You can create a second persistence unit in your configuration, but that doesn't necessarily mean you should. Multiple PUs are of course entirely right and proper, but I'd steer clear of mixing them up when they're specifically for different environments, e.g. production versus test.

In your instance, I'd have two persistence configuration files, and have Ant / Maven / build tool of choice copy / rename the correct one when appropriate.

Ben
  • 7,548
  • 31
  • 45