web-dev-qa-db-fra.com

JPA avec erreur Hibernate: [PersistenceUnit: JPA] Impossible de générer EntityManagerFactory

J'ai un problème avec Java Persistence API et Hibernate . Ma situation de projet est:

enter image description here

Mon fichier persistence.xml est:

<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>

Et je crée EntityManagerFactory dans:

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());
        }
    }

Mon erreur concerne la création de 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

Une partie du code des classes Libro et Categoria est:

@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;

....

et 

@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;

....

Mon fichier de configuration Hibernate est:

<?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>

Toutes les idées !! Merci !!

12
Davidin073

Vous n'avez pas besoin à la fois de hibernate.cfg.xml et de persistence.xml dans ce cas. Avez-vous essayé de supprimer hibernate.cfg.xml et de tout mapper dans persistence.xml uniquement?

Mais comme l’a souligné l’autre réponse, ce n’est pas correct comme ceci:

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

Vous ne voulez pas utiliser @Column à la place?

13
Balázs Németh

Supprimez la @JoinColumn(name="categoria") dans le champ ID de la classe Categoria et je pense que cela fonctionnera.

1
eternay

Utilisez l'annotation ci-dessus si quelqu'un est confronté: - au fournisseur de persistance org.hibernate.jpa.HibernatePersistenceProvider lorsqu'il a tenté de créer la fabrique de gestionnaires d'entités de conteneur pour l'unité de persistance paymentenginePU. L'erreur suivante s'est produite: [PersistenceUnit: paymentenginePU] Impossible de générer Hibernate SessionFactory ** Ceci est une solution si vous utilisez la table Audit. @ Audit 

Utilisez: - @Audited (targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) dans la superclasse.

0
Vinit Bhardwaj

Cela a fonctionné pour moi après avoir ajouté la dépendance suivante dans pom,

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

0
Bharath Valse