Tuesday, June 19, 2012

JBAS011440: Can't find a persistence unit named null in deployment

Encountering the deployment error "JBAS011440: Can't find a persistence unit named null in deployment" on JBoss AS 7? Using Arquillian?

You probably put your persistence.xml in the wrong place in your archive. Try printing the archive contents.



@RunWith(Arquillian.class)
public class SomeTest {

 @Deployment
 public static WebArchive createDeployment() {
  WebArchive war = ShrinkWrap.create(WebArchive.class)
    .addAsResource(new File("src/main/resources/META-INF/persistence.xml"), "META-INF/persistence.xml")
    .addAsWebInfResource(new File("src/main/webapp/WEB-INF/beans.xml"), "beans.xml");
  war.writeTo(System.err, org.jboss.shrinkwrap.api.formatter.Formatters.VERBOSE);
  System.err.println();
  return war;
 }

 @Test
 public void blah() {
   // ... 
 }
}

Note in the above that I've used addAsResource(..., "META-INF/persistence.xml"). In a web archive, addAsManifestResource(..., "persistence.xml") won't work as it'll place it in /META-INF/persistence.xml and the correct (albeit very very strange) location is /WEB-INF/classes/META-INF/persistence.xml

It's a stupid mistake, but when you can't directly see the archive structure it's not always immediately obvious. JBoss's error message doesn't help - "persistence unit named null" ?.

Hopefully a future ShrinkWrap release will add an archive-format-aware API for placing persistence.xml in its wacky location.

All the below are wrong:
  • addAsWebInfResource(..., "persistence.xml") -> /WEB-INF/persistence.xml
  • addAsWebInfResource(..., "META-INF/persistence.xml") -> /WEB-INF/META-INF/persistence.xml
  • addAsManifestResource(..., "persistence.xml") -> /META-INF/persistence.xml
  • addAsResource(..., "persistence.xml") -> /WEB-INF/classes/persistence.xml
If in doubt, use writeTo(...) as shown above to print your archive structure and make sure persistence.xml is in /WEB-INF/classes/META-INF/persistence.xml

1 comment:

Captchas suck. Bots suck more. Sorry.