[eclipse] JPA With Hibernate Error: [PersistenceUnit: JPA] Unable to build EntityManagerFactory

I have a problem with Java Persistence API and Hibernate. My situation of project is:

enter image description here

My persistence.xml file is:

<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="JPA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.david.Libro</class>
        <class>com.david.Categoria</class>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="javax.persistence.transactionType" value="RESOURCE_LOCAL" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/arquitecturaJava" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        </properties>
    </persistence-unit>
</persistence>

And i create EntityManagerFactory in:

private static EntityManagerFactory buildEntityManagerFactory() 
    {
        try 
        {
            return Persistence.createEntityManagerFactory("JPA");
        } 
        catch (Throwable ex) 
        {
                    ex.printStackTrace();
            //throw new RuntimeException("Error al crear la factoria de JPA:->"+ ex.getMessage());
        }
    }

My error is about create EntityManagerFactory:

    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.<clinit>(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.<init>(EntityManagerFactoryImpl.java:96)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
    ... 27 more

Part of Libro and Categoria classes code is:

@Entity
@Table(name = "Categorias")
public class Categoria implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    @JoinColumn(name = "categoria") 
    private String id;
    private String descripcion;

....

and

@Entity
@Table(name="Libros")
public class Libro implements Serializable
{
    private static final long serialVersionUID = 1L;
    
    @Id
    private String isbn;
    private String titulo;
    @ManyToOne
    @JoinColumn (name="categoria")
    private Categoria categoria;

....

My file of Hibernate Configuration is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/arquitecturajava</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.pool_size">5</property>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property>
        
        <mapping class="com.david.Categoria"></mapping>
        <mapping class="com.david.Libro"></mapping>
        
    </session-factory>
</hibernate-configuration>

Any ideas!! ThankĀ“s!!

This question is related to eclipse hibernate jpa-2.0 entitymanager

The answer is


Suppress the @JoinColumn(name="categoria") on the ID field of the Categoria class and I think it will work.


Use above annotation if someone is facing :--org.hibernate.jpa.HibernatePersistenceProvider persistence provider when it attempted to create the container entity manager factory for the paymentenginePU persistence unit. The following error occurred: [PersistenceUnit: paymentenginePU] Unable to build Hibernate SessionFactory ** This is a solution if you are using Audit table.@Audit

Use:- @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) on superclass.


It worked for me after adding the following dependency in pom,

<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-validator</artifactId>
 <version>4.3.0.Final</version>
</dependency>

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to hibernate

Hibernate Error executing DDL via JDBC Statement How does spring.jpa.hibernate.ddl-auto property exactly work in Spring? Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed JPA Hibernate Persistence exception [PersistenceUnit: default] Unable to build Hibernate SessionFactory Disable all Database related auto configuration in Spring Boot Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] HikariCP - connection is not available Hibernate-sequence doesn't exist How to find distinct rows with field in list using JPA and Spring? Spring Data JPA and Exists query

Examples related to jpa-2.0

JPA Query selecting only specific columns without using Criteria Query? JPA With Hibernate Error: [PersistenceUnit: JPA] Unable to build EntityManagerFactory How to define unidirectional OneToMany relationship in JPA JPA CriteriaBuilder - How to use "IN" comparison operator JPA: unidirectional many-to-one and cascading delete How to properly express JPQL "join fetch" with "where" clause as JPA 2 CriteriaQuery? JPA 2.0, Criteria API, Subqueries, In Expressions JPA Criteria API - How to add JOIN clause (as general sentence as possible) In JPA 2, using a CriteriaQuery, how to count results JPA CascadeType.ALL does not delete orphans

Examples related to entitymanager

What does EntityManager.flush do and why do I need to use it? JPA With Hibernate Error: [PersistenceUnit: JPA] Unable to build EntityManagerFactory The EntityManager is closed What is the best way to update the entity in JPA Create JPA EntityManager without persistence.xml configuration file JPA EntityManager: Why use persist() over merge()?