0

I am using JPA for Hibernate. In one of my service method where records will be inserted. I have the following line to initiate EntityManger:

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("CghubDB");
    EntityManager manager = factory.createEntityManager();
    CghubDao dao = new CghubDao(manager);

When I was debugging the code, I notice Hibernate doing weird things while executing Persistence.createEntityManagerFactory. The output is as belows:

**Mar 08, 2016 11:42:45 AM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: CghubDB
    ...]
Mar 08, 2016 11:42:45 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
Mar 08, 2016 11:42:45 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 08, 2016 11:42:45 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mar 08, 2016 11:42:45 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 08, 2016 11:42:45 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Mar 08, 2016 11:42:45 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://mdarisrac02d:3306/pancancer]
Mar 08, 2016 11:42:45 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=dian, password=****}
Mar 08, 2016 11:42:45 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 08, 2016 11:42:45 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Mar 08, 2016 11:42:46 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Mar 08, 2016 11:42:46 AM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Hibernate: alter table PAIR_TB drop foreign key FK8s20mxutpctn5n8hm9bvg8iyd
Hibernate: alter table SEQ_TB drop foreign key FKkr6f9ff4d9h4pds6cby5ucky0
Hibernate: drop table if exists GROUP_TB
Hibernate: drop table if exists PAIR_TB
Hibernate: drop table if exists SEQ_TB
Hibernate: create table GROUP_TB (ROW_ID integer not null, GROUP_NAME varchar(255), primary key (ROW_ID))
Hibernate: create table PAIR_TB (ROW_ID integer not null, PAIR_CODE varchar(255), GROUP_ID integer, primary key (ROW_ID))
Hibernate: create table SEQ_TB (ROW_ID integer not null, ALIQUOT_ID varchar(255), ANALYSIS_ID varchar(255), ANALYTE_CODE varchar(255), center varchar(255), checksum varchar(255), DATA_URL varchar(255), DCC_PROJECT varchar(255), DCC_SPECIMEN_TYPE varchar(255), DISEASE_ABBR varchar(255), DISEASE_FULL varchar(255), filename varchar(255), filepath varchar(255), filesize integer not null, LAST_MODIFIED datetime, LEGACY_ID varchar(255), PARTICIPANT_ID varchar(255), platform varchar(255), PUBLISHED_DATE datetime, refassem varchar(255), SAMPLE_ID varchar(255), SAMPLE_TYPE varchar(255), SEQ_FORMAT varchar(255), SEQ_SOURCE varchar(255), SPECIMEN_ID varchar(255), study varchar(255), TISSUE_TYPE varchar(255), TSS_ID varchar(255), PAIR_ID integer, primary key (ROW_ID))
Hibernate: alter table PAIR_TB add constraint FK8s20mxutpctn5n8hm9bvg8iyd foreign key (GROUP_ID) references GROUP_TB (ROW_ID)
Hibernate: alter table SEQ_TB add constraint FKkr6f9ff4d9h4pds6cby5ucky0 foreign key (PAIR_ID) references PAIR_TB (ROW_ID)
Mar 08, 2016 11:42:47 AM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@420f11e'
Mar 08, 2016 11:42:47 AM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory**

I don't understand why it is trying to drop all the tables and create them again. Same thing with the foreign keys. The entity classes were generated automatically in Eclipse.

persistence.xml

<?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="CghubDB" >
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>org.mdacc.rists.cghub.model.GroupTb</class>
        <class>org.mdacc.rists.cghub.model.PairTb</class>
        <class>org.mdacc.rists.cghub.model.SeqTb</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://risurl:3306/pancancer" />
            <property name="javax.persistence.jdbc.user" value="user" />
            <property name="javax.persistence.jdbc.password" value="pass" />

            <!--Hibernate properties-->
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />
        </properties>
    </persistence-unit>
</persistence>
Nasreddin
  • 1,509
  • 9
  • 31
  • 36

1 Answers1

1

You must set the hibernate property "hibernate.hbm2ddl.auto" to another value. Either validate, update, create or create-drop.

A more detailed explanation can be found at Hibernate hbm2ddl.auto possible values and what they do?

Community
  • 1
  • 1
mh-dev
  • 5,264
  • 4
  • 25
  • 23
  • I changed the value to update and validate in my persistence.xml, and reran the program. Still dropping and creating tables. – Nasreddin Mar 08 '16 at 19:21
  • Can you add your persistence.xml file (without credientials) – mh-dev Mar 08 '16 at 19:23
  • persistence.xml added in original post – Nasreddin Mar 08 '16 at 19:26
  • Hmm you can try to set the value="" for that property. But this does not make sense to me. It seems that someone else overwrites that property. Do you have a hibernate config file? – mh-dev Mar 08 '16 at 19:33
  • I used to have a hibernate.cfg file, but I deleted it since I converted to JPA. I just looked through the whole directory, no hibernate.cfg file. This JPA project is my dao module. I am running the method to persist records in a service module. Both modules are under a parent project. Would that be a factor? – Nasreddin Mar 08 '16 at 19:46
  • Sorry I do not have another idea, its hard without the actual code. – mh-dev Mar 08 '16 at 19:51
  • I created a test class to insert single record in both dao and service module. While both works for the insert, the class in dao did not drop and recreate the table like service module did. dao is the module where I have persistence.xml. When I change the value of hbm2ddl.auto, it does have different effect on dao. – Nasreddin Mar 08 '16 at 20:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105735/discussion-between-mh-dev-and-nasreddin). – mh-dev Mar 08 '16 at 20:08