Gert
Gert

Reputation: 230

How do I find what persistence.xml is active

I have inherited a maven based project that was created with Netbeans 7.4. I have configured a local Glassfish 3.1.2.2 in the servers tab, set up JDBC connection pools and resources and can debug the project correctly. However, I created 2 more tables in one of the connected databases, and let Netbeans generate 2 new entities from the Database schema. The entities are generated correctly, but when I start using them and start a debug session on the glassfish server I can see error messages being logged saying that the entities are not declared in the persistence unit.

I am trying to find what persistence.xml is being used, but although I find a lot of old ones inside the project structure (that I didn't create myself and I don't have contact with the authors) I cannot find any that seems to be the one being used. Hibernate only refers to the Persistence Unit name, not to the path of the file being used.

I am new to Java Persistence, so I am a bit confused. Can anybody suggest how I find, or recreate, the persistence.xml file?

EDIT: Here is the stack trace:

INFO: HHH000412: Hibernate Core {4.2.6.Final}
INFO: HHH000206: hibernate.properties not found
INFO: HHH000021: Bytecode provider name : javassist
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: core_rw_pu_hibernate
    ...]
INFO: HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
SEVERE: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
SEVERE: Exception while preparing the app
SEVERE: [PersistenceUnit: core_rw_pu_hibernate] Unable to build EntityManagerFactory
javax.persistence.PersistenceException: [PersistenceUnit: core_rw_pu_hibernate] 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.createContainerEntityManagerFactory(HibernatePersistence.java:76)
    at org.glassfish.persistence.jpa.PersistenceUnitLoader.loadPU(PersistenceUnitLoader.java:206)
    at org.glassfish.persistence.jpa.PersistenceUnitLoader.<init>(PersistenceUnitLoader.java:120)
    at org.glassfish.persistence.jpa.JPADeployer$1.visitPUD(JPADeployer.java:224)
    at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:495)
    at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:233)
    at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:871)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:410)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:389)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:461)
    at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:212)
    at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179)
    at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    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:744)
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.acme.ee.domain.entity.DeviceEntity.deviceInOuts[com.acme.ee.domain.entity.DeviceInOuts]
    at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1059)
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:733)
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:668)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:69)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1632)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1390)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1777)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
    ... 38 more

Upvotes: 0

Views: 2331

Answers (2)

Omid Ashouri
Omid Ashouri

Reputation: 123

your persistence.xml should have information about your connection to the database like bellow:

 <?xml version="1.0" encoding="UTF-8"?>
 <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
 <persistence-unit name="YOUR_PERSISTENCE_UNIT_NAME" transaction-type="JTA">
 <provider>org.hibernate.ejb.HibernatePersistence</provider>
 <jta-data-source>jdbc/YOUR_JNDI_NAME</jta-data-source>
 <exclude-unlisted-classes>false</exclude-unlisted-classes>
 <properties>
 <!--<property name="javax.persistence.schema-generation.database.action" value="none"/> -->
 <property name="hibernate.archive.autodetection" value="class"/>
 <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
 <property name="hibernate.transaction.jta.platform"  value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
 <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/YOUR_SCHEMA_NAME?zeroDateTimeBehavior=convertToNull"/>
 <property name="hibernate.connection.username" value="YOUR_USERNAME"/>
 <property name="hibernate.connection.password" value="YOUR_PASSWORD"/>
 <property name="hibernate.show_sql" value="true"/>
 <property name="hibernate.hbm2ddl.auto" value="update" /> 
 </properties>
 </persistence-unit>
 </persistence>

Upvotes: 0

unwichtich
unwichtich

Reputation: 13857

In a normal maven-based web application project the persistence.xml has to be placed in the folder:

src/main/resources/META-INF

In the NetBeans Projects tab this folder can be seen by opening "Other Sources" (should be right under "Source Packages").

If this isn't the right file, you have the option to search for the name of the PersistenceUnit in the whole project. The name has to be declared in the persistence.xml. If it finds multiple files you can find the one which is actually used by making the files unparsable one by one. To do this, just remove a closing tag (like </provider>) in the first file, save it and deploy it. If it throws an error which tells you the file is unparsable (probably something containing the class SAXParser), this is the file which is used. If it doesn't throw an error, try again with the next file.

To fix the initial problem (new entities not in persistence unit) make sure you add the new entity classes in the persistence.xml like this:

<class>com.company.SomeClass</class>

Another option is to add the following to your persistence.xml:

<exclude-unlisted-classes>false</exclude-unlisted-classes>

This should automatically discover all entities, but sometimes this doesn't work because the project structure doesn't fit, then you have to explicitly list every entity.

Upvotes: 1

Related Questions