Hibernate中的javax.persistence.PersistenceException

时间:2013-07-29 08:45:14

标签: persistence java-6 hibernate-4.x

我已经从数据库生成了POJO和相应的hbm.xml映射文件。我想测试这是否正常工作,所以我创建了一个简单的HelloWorld,它读取一个表中的所有数据并打印出来。当我尝试运行它时,抛出PersistenceException。这是我的StackTrace:

javax.persistence.PersistenceException: [PersistenceUnit: helloworld] 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(Unknown Source)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    at de.planet.hibernateHelloWorld.HelloWorld.service(HelloWorld.java:51)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.hibernate.AnnotationException: Use of the same entity name twice: NnsNetzdaten
    at org.hibernate.cfg.annotations.EntityBinder.bindEntity(EntityBinder.java:403)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:585)
    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)
    ... 30 more
Caused by: org.hibernate.DuplicateMappingException: duplicate import: NnsNetzdaten refers to both de.planet.hibernateHelloWorld.data.NnsNetzdaten and NnsNetzdaten (try using auto-import="false")
    at org.hibernate.cfg.Configuration$MappingsImpl.addImport(Configuration.java:2646)
    at org.hibernate.cfg.annotations.EntityBinder.bindEntity(EntityBinder.java:396)
    ... 37 more

我正在直接从Eclipse Kepler将应用程序部署到Glassfish 3服务器。但是,当我使用Maven打包和部署应用程序时,也会出现此问题。

这是我的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_1_0.xsd"
    version="1.0">
    <persistence-unit name="helloworld">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.archive.autodetection"
                value="class, hbm" />
            <property name="hibernate.connection.driver_class"
                value="oracle.jdbc.driver.OracleDriver" />
            <property name="hibernate.connection.password"
                value="**********" />
            <property name="hibernate.connection.url"
                value="jdbc:oracle:thin:@d11503oracle1.planet-ic.de:1521:XE" />
            <property name="hibernate.connection.username"
                value="WEMAG_ENET_DEV" />
            <property name="hibernate.default_schema" value="WEMAG_ENET_DEV" />
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.Oracle10gDialect" />
            <property name="hibernate.c3p0.max_size" value="30" />
            <property name="hibernate.c3p0.min_size" value="5" />
            <property name="hibernate.c3p0.idle_test_period"
                value="3000" />
            <property name="hibernate.c3p0.timeout" value="300" />
            <property name="hibernate.c3p0.max_statements"
                value="50" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

每个POJO类在类定义之前还有以下注释:

@Entity
@Table(name = "[Table Name]")

我不认为这些是必要的,所以我尝试删除它们,但是这会引起各种各样的问题所以我认为不是这样的:p

编辑: 它现在有效,请参阅答案,但我不知道为什么。 Hibernate不再检测hbm.xml映射数据,因为POJO类不包含任何映射数据,它不应该能够从数据库中获取数据: - /。

1 个答案:

答案 0 :(得分:0)

显然正在改变

<property name="hibernate.archive.autodetection"
                value="class, hbm" />

<property name="hibernate.archive.autodetection"
                value="class" />

我不完全确定原因:p