Question

I have generated POJO's and corresponding hbm.xml mapping files from a database. I wanted to test whether this worked correctly so I created a simple HelloWorld that reads all the data in one Table and prints it. When I attempt to run this, a PersistenceException is thrown. Here is my 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

I am deploying the application to a Glassfish 3 server directly from Eclipse Kepler. However this problem also occurs when I package and deploy the application using Maven.

Here is my 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>

Each POJO class also has the following annotations before the class definition:

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

I didn't think these were necessary, so I tried removing them, but this cause all sorts of problems so I don't think that's the way to go :p

EDIT: It works now, See Answer, but I don't know why. Hibernate is no longer detecting the hbm.xml mapping data so, since the POJO classes don't contain any mapping data, it should not be able to fetch data from the database :-/.

Was it helpful?

Solution

Apparently changing

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

to

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

I am not entirely certain why :p

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top