title: JPA With Hibernate Error: [PersistenceUnit: JPA] Unable to build EntityManagerFactory
body: <p>I have a problem with Java Persistence API and Hibernate.
  My situation of project is:</p>
  <p><img src="https://i.stack.imgur.com/Z3vgG.png" alt="enter image description here" /></p>
  <p>My persistence.xml file is:</p>
  <pre><code>&lt;persistence 
      xmlns=&quot;http://java.sun.com/xml/ns/persistence&quot;
      xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd&quot;
      version=&quot;2.0&quot;&gt;
      &lt;persistence-unit name=&quot;JPA&quot;&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
          &lt;class&gt;com.david.Libro&lt;/class&gt;
          &lt;class&gt;com.david.Categoria&lt;/class&gt;
          &lt;properties&gt;
              &lt;property name=&quot;hibernate.show_sql&quot; value=&quot;true&quot; /&gt;
              &lt;property name=&quot;javax.persistence.transactionType&quot; value=&quot;RESOURCE_LOCAL&quot; /&gt;
              &lt;property name=&quot;javax.persistence.jdbc.driver&quot; value=&quot;com.mysql.jdbc.Driver&quot; /&gt;
              &lt;property name=&quot;javax.persistence.jdbc.url&quot; value=&quot;jdbc:mysql://localhost/arquitecturaJava&quot; /&gt;
              &lt;property name=&quot;javax.persistence.jdbc.user&quot; value=&quot;root&quot; /&gt;
              &lt;property name=&quot;javax.persistence.jdbc.password&quot; value=&quot;root&quot; /&gt;
              &lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.MySQL5Dialect&quot; /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  <p>And i create EntityManagerFactory in:</p>
  <pre><code>private static EntityManagerFactory buildEntityManagerFactory() 
      {
          try 
          {
              return Persistence.createEntityManagerFactory(&quot;JPA&quot;);
          } 
          catch (Throwable ex) 
          {
                      ex.printStackTrace();
              //throw new RuntimeException(&quot;Error al crear la factoria de JPA:-&gt;&quot;+ ex.getMessage());
          }
      }
  </code></pre>
  <p>My error is about create EntityManagerFactory:</p>
  <pre><code>    javax.persistence.PersistenceException: [PersistenceUnit: JPA] Unable to build EntityManagerFactory
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:924)
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899)
      at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:59)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
      at com.david.JPAHelper.buildEntityManagerFactory(JPAHelper.java:14)
      at com.david.JPAHelper.&lt;clinit&gt;(JPAHelper.java:8)
      at com.david.Categoria.buscarTodos(Categoria.java:93)
      at com.david.FormularioInsertarLibroAccion.ejecutar(FormularioInsertarLibroAccion.java:25)
      at com.david.ControladorLibros.doGet(ControladorLibros.java:38)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping com.david.Libro
      at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2638)
      at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:706)
      at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3512)
      at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3466)
      at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1756)
      at org.hibernate.ejb.EntityManagerFactoryImpl.&lt;init&gt;(EntityManagerFactoryImpl.java:96)
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
      ... 27 more
  </code></pre>
  <p>Part of Libro and Categoria classes code is:</p>
  <pre><code>@Entity
  @Table(name = &quot;Categorias&quot;)
  public class Categoria implements Serializable
  {
      private static final long serialVersionUID = 1L;
  
      @Id
      @JoinColumn(name = &quot;categoria&quot;) 
      private String id;
      private String descripcion;
  
  ....
  </code></pre>
  <p>and</p>
  <pre><code>@Entity
  @Table(name=&quot;Libros&quot;)
  public class Libro implements Serializable
  {
      private static final long serialVersionUID = 1L;
      
      @Id
      private String isbn;
      private String titulo;
      @ManyToOne
      @JoinColumn (name=&quot;categoria&quot;)
      private Categoria categoria;
  
  ....
  </code></pre>
  <p>My file of Hibernate Configuration is:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot; &quot;http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd&quot;&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name=&quot;connection.driver_class&quot;&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name=&quot;connection.url&quot;&gt;jdbc:mysql://localhost/arquitecturajava&lt;/property&gt;
          &lt;property name=&quot;connection.username&quot;&gt;root&lt;/property&gt;
          &lt;property name=&quot;connection.password&quot;&gt;root&lt;/property&gt;
          &lt;property name=&quot;connection.pool_size&quot;&gt;5&lt;/property&gt;
          &lt;property name=&quot;dialect&quot;&gt;org.hibernate.dialect.MySQL5Dialect&lt;/property&gt;
          &lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;
          
          &lt;mapping class=&quot;com.david.Categoria&quot;&gt;&lt;/mapping&gt;
          &lt;mapping class=&quot;com.david.Libro&quot;&gt;&lt;/mapping&gt;
          
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  <p>Any ideas!!
  Thank´s!!</p>