JPA与Hibernate错误:[PersistenceUnit:JPA]无法构建EntityManagerFactory

时间:2013-06-04 15:55:46

标签: eclipse hibernate jpa-2.0 entitymanager

我遇到了Java Persistence API和Hibernate的问题。 我的项目情况是:

enter image description here

我的persistence.xml文件是:

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

我在:

创建了EntityManagerFactory
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());
        }
    }

我的错误是关于创建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

Libro和Categoria类代码的一部分是:

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

....

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

....

我的Hibernate配置文件是:

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

任何想法!! Thank's !!

4 个答案:

答案 0 :(得分:13)

在这种情况下,您不需要hibernate.cfg.xmlpersistence.xml。您是否尝试删除hibernate.cfg.xml并仅在persistence.xml中映射所有内容?

但正如另一个答案也指出的那样,这不是这样的:

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

您不想使用@Column吗?

答案 1 :(得分:1)

取消@JoinColumn(name="categoria")课程的ID字段上的Categoria,我认为它会有效。

答案 2 :(得分:0)

在pom中添加以下依赖项后,它对我有用,

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

答案 3 :(得分:0)

如果有人面临以下情况,请使用上述注释: - org.hibernate.jpa.HibernatePersistenceProvider持久性提供程序,当它尝试为paymentenginePU持久性单元创建容器实体管理器工厂时。发生以下错误:[PersistenceUnit:paymentenginePU]无法构建Hibernate SessionFactory **如果您使用的是Audit表,这是一个解决方案。@ Audit

使用: - 超类上的@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)。