1 Reply Latest reply on Apr 6, 2015 4:30 PM by pmorganca

    problems with persistence.xml and hibernate.cfg.xml in the same project

    pmorganca

      Problem:

      I've got a datasource in hibernate.cfg.xml that seems to be using properties from persitence.xml. I can see in the server (JBoss EAP 6.2.0) console, messages like these:

      15:32:33,372 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 116) HHH000389: Unsuccessful: create table HR.reduction (months_num smallint not null, reduction_amt decimal(10,5), primary key (months_num))

      15:32:33,372 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 116) Schema "HR" not found; SQL statement:

      create table HR.reduction (months_num smallint not null, reduction_amt decimal(10,5), primary key (months_num)) [90079-168]

       

      The above queries are attempted because of the property <property name="hibernate.hbm2ddl.auto" value="create-drop" /> in persistence.xml, but they are being attempted on the datasource that is configured by hibernate.cfg.xml.

       

      What I think I need is a way to have a JPA configured datasource through persitence.xml and a Hibernate configured datasource through hibernate.cfg.xml, and knowledge of how to keep them separate.

       

      I definitely have a  lack of JPA and Hibernate knowledge, but since the whole app was created by Jboss Developer Studio wizards I thought this forum was an appropriate place to have a discussion about it.

       

      Background:

      I started a web app project using the maven archetype org.jboss.spec.archetypes:jboss-javaee6-ear-webapp and imported it into JBoss Developer Studio 7.1.1 GA. That creates a basic "register members" application using an example HSQLDB local filesystem data source. I renamed the Hibernate Configuration for this datasource to MemberDbQuickStart in the IDE (see attached screenshot).

      I also configured an SQL Server data source and reverse engineered a few tables.

      I've done all this mostly with Maven and Eclipse wizards.

      You can see in the Hibernate Configurations tab of the Hibernate Perspective that classes from the reverse engineered Compare-ejb configuration are showing up in the MemberDbQuickStart configuration. However, neither configuration has additional mappings in the configuration properties, the mappings are being picked up automatically - obviously through the *.hbm.xml  files that were created by the reverse engineering wizard.

      Capture.JPG

      The classes are in the MemberDbQuickStart configuration but the Database is in the correct Compare-ejb configuration.


      Here's the persitence.xml file:

      <?xml version="1.0" encoding="UTF-8"?>

      <persistence version="2.0"

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

         <persistence-unit name="primary">

            <!-- If you are running in a production environment, add a managed

               data source, the example data source is just for proofs of concept! -->

            <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>

            <properties>

               <!-- Properties for Hibernate -->

               <property name="hibernate.hbm2ddl.auto" value="create-drop" />

               <property name="hibernate.show_sql" value="false" />

            </properties>

         </persistence-unit>

      </persistence>

       

      Here's the hibernate.cfg.xml:

      <?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 name="HRSessionFactory">

              <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>

              <property name="hibernate.connection.password">asIF!</property>

              <property name="hibernate.connection.url">jdbc:jtds:sqlserver://noWay:50012/HR</property>

              <property name="hibernate.connection.username">yaRight</property>

              <property name="hibernate.default_schema">dbo</property>

              <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

          </session-factory>

      </hibernate-configuration>


      I will eventually be removing the MemberDbQuickStart components of the app that was created for me by the Archetype and I suspect that will make my problem go away.

      BUT I want to understand the architecture and the different frameworks I'm using before I do that. I'd like to have both datasources active with no errors, I don't think it's unheard of to have two datasources in one web app.

        • 1. Re: problems with persistence.xml and hibernate.cfg.xml in the same project
          pmorganca

          I'm pretty sure the solution to this will be in injecting a EntityManagers by persistence unit name using @PersistenceContext(name="primary")  or other name... but I'm still working on a solution.

           

          So far, I have worked around this: as I mentioned the MemberDbQuickStart components were temporary anyway. I removed them and now only have one persistence unit - so this question is still open.