0

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', 'john.smith@mailinator.com', '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?
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user697911
  • 10,043
  • 25
  • 95
  • 169

1 Answers1

3

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?
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • Adding onto that: Suppose you had a table existing that customers use for your enterprise level product. If now you are using Hibernate (maybe something else used before?), you probably want that data to still exist and in your new tables managed and created by Hibernate. So yeah, Hibernate team was considering the case of someone horizontally shifting to Hibernate from some other mechanism. Look up liquibase and it's power to see an alternative to this – rosenthal Feb 24 '16 at 01:26
  • "From your entities/entity configurations.", do you mean it creates the table based on the Member class's annotation? Because I don't see any other XML configuration in the application, except for the persistence.xml to configure the database. Please see the added Member class definition. Does this class alone ask the hibernate to create the table? – ling Feb 24 '16 at 03:00
  • @ling Are your running multiple accounts? Why is user _user697911_ asking a question, and user _ling_ providing the details? – Robby Cornelissen Feb 24 '16 at 03:12
  • Yes. two computers. one is home and the other is in office. Thank you. – ling Feb 24 '16 at 03:16
  • @ling To answer your question: yes based on the annotations. – Robby Cornelissen Feb 24 '16 at 03:19
  • @RobbyCornelissen, what about if I already have the Member table created manually in mysql? I don't want hibernate to create the table. I only wants it to insert and retrieve data from it. – ling Feb 24 '16 at 03:20
  • Don't set the `hibernate.hbm2ddl.auto` to `create`. – Robby Cornelissen Feb 24 '16 at 03:21
  • Should I use "update" value? Also, would it serve the same purpose if I get rid of the @Table annotation from the Member class? – ling Feb 24 '16 at 03:24
  • I don't want to be rude or offensive, but the questions you're asking now are things that you can easily find out by reading the documentation or trying things yourself. – Robby Cornelissen Feb 24 '16 at 03:25
  • Also, if I don't set the "create" value, what about if I have another class and that class needs to create a table automatically by Hibernate? – ling Feb 24 '16 at 03:25