2 Replies Latest reply on Dec 29, 2011 10:26 PM by rnott

    Can't find a deployment unit in .war file.  Persistence.xml issue.

    aarnold

      I am trying to use hibernate with mysql for data pesistence.  I am running JBoss AS7.  When the container goes to load my EntityManager, I get this error:  "java.lang.RuntimeException: Can't find a deployment unit named  at deployment "myProject.war"".

       

      I looked extensively online for a solution.  Most problems seem to deal with .ear archives.  I am not packaging in an .ear, I am packaging is a .war.  In fact I am using the appraoch described in the jboss as7 quick start guide.  ( I am not using the ejb apprach). 

       

      Here is my persistence.xml:

       

      <?xml version="1.0" encoding="UTF-8"?>

      <persistence xmlns="http://java.sun.com/xml/ns/persistence"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"

         version="2.0">

         <persistence-unit name="myDatabase">

            <provider>org.hibernate.ejb.HibernatePersistence</provider>

            <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>

            <properties>

               <property name="hibernate.hbm2ddl.auto" value="create-drop" />

            </properties>

         </persistence-unit>

      </persistence>

       

      Here is my Resources.java (class producing the EntityManager):

       

      package com.missionse.de.coda.data;

       

       

      import java.util.logging.Logger;

       

       

      import javax.enterprise.inject.Produces;

      import javax.enterprise.inject.spi.InjectionPoint;

      import javax.persistence.EntityManager;

      import javax.persistence.PersistenceContext;

       

       

      public class Resources {

       

       

         // Expose an entity manager using the resource producer pattern

         @SuppressWarnings("unused")

         @PersistenceContext(name = "myDatabase")

         @Produces

         private EntityManager em;

       

       

         @Produces

         Logger getLogger(InjectionPoint ip) {

            String category = ip.getMember().getDeclaringClass().getName();

            return Logger.getLogger(category);

         }

       

       

      }

        • 1. Re: Can't find a deployment unit in .war file.  Persistence.xml issue.
          aarnold

          continued....

           

          Here is my interface:

           

          package com.missionse.de.coda.data;

           

           

          import java.util.List;

           

           

          public interface DataManager {

           

           

             public String addData(Data data) throws Exception;

           

           

             public Data findData(String username) throws Exception;

           

           

          }

           

           

          Here is my implementation:

           

          package com.missionse.de.coda.data;

           

           

          import java.util.List;

          import java.util.logging.Logger;

           

           

          import javax.enterprise.context.RequestScoped;

          import javax.inject.Inject;

          import javax.inject.Named;

          import javax.persistence.EntityManager;

          import javax.transaction.UserTransaction;

           

           

          @Named("dataManager")

          @RequestScoped

          public class ManagedBeanDataManager implements DataManager {

           

           

             @Inject

             private transient Logger logger;

           

           

             @Inject

             private EntityManager userDatabase;

           

           

             @Inject

             private UserTransaction utx;

           

           

             public String addData(Data data) throws Exception {

                try {

                   try {

                      utx.begin();

                      userDatabase.persist(data);

                      logger.info("Added " + data);

                   } finally {

                      utx.commit();

                   }

                } catch (Exception e) {

                   utx.rollback();

                   throw e;

                }

                return "userAdded";

             }

           

           

             public Data findData(String name) throws Exception {

                try {

                   try {

                      utx.begin();

                      @SuppressWarnings("unchecked")

                      List<Data> results = userDatabase

                            .createQuery("select u from User u where u.username=:username and u.password=:password")

                            .setParameter("name",name).getResultList();

                      if (results.isEmpty()) {

                         return null;

                      } else if (results.size() > 1) {

                         throw new IllegalStateException("Cannot have more than one user with the same username!");

                      } else {

                         return results.get(0);

                      }

                   } finally {

                      utx.commit();

                   }

                } catch (Exception e) {

                   utx.rollback();

                   throw e;

                }

           

           

             }

           

           

          }

           

          When I try to use it like this I get the error....

           

          ...

           

          @Inject

          DataManager dm;

           

          ...

           

          dm.addData(new Data());

          • 2. Re: Can't find a deployment unit in .war file.  Persistence.xml issue.
            rnott

            Based on your error message, I'd guess the container is not finding your persistence.xml file. Here are the rules on where the container will search:  https://docs.jboss.org/author/display/AS7/JPA+Reference+Guide

             

            Beware that the rule which states WEB-INF/classes will be searched appears to be false, at least in the version I'm using (7.1.0.Beta1b). In my case, I simply add a new JAR to WEB-INF/lib whose only contents are META-INF/persistence.xml and it's suddenly found.