user697911
user697911

Reputation: 10531

How does Hibernate hbm2ddl automatically create table?

I am reading an example of Java EE (JBoss) application, and learning the basics of Hibernate in Java EE. Under src/main/resources/META-INF/persistence.xml:

      <jta-data-source>java:jboss/datasources/MemberDS</jta-data-source>
      <properties>
         <!-- Properties for Hibernate -->
         <property name="hibernate.hbm2ddl.auto" value="create" />
         <property name="hibernate.show_sql" value="false" />
      </properties>
   </persistence-unit>

Under src/main/resources/import.sql:

insert into Member (id, name, email, password, phone_number) values (0, 'John Smith', '[email protected]', 'password', '2125551212')

Under model package, it has a Member class.

My questions:

  1. how does the application automatically create the Table Member in database?
  2. Where does it get the schema information?
  3. Why and how 'import.sql' is atomically executed once the application is running?

Upvotes: 0

Views: 1446

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97162

The answers to your questions, in order:

  1. How does the application automatically create the Member table in database?
    By executing a CREATE TABLE statement.
  2. Where does it get the schema information?
    From your entities/entity configurations.
  3. Why and how is import.sql automically executed once the application is running?
    As to the how: Hibernate will automatically try to import a import.sql file from the root of your classpath if the hibernate.hbm2ddl.auto property is set to create (files to be imported can be further specified using the hibernate.hbm2ddl.import_files property).
    As to the why: I can only guess, but I'm guessing that the Hibernate team thought it might be a convenient feature?

Upvotes: 3

Related Questions