5 Replies Latest reply on Sep 6, 2006 9:38 PM by Gavin King

    when and how does the seam create  tables in the database?

    leon qian Newbie

      hi, anybody, who can tell me about when and how the seam create tables in the database?
      i think the "import.sql" is executed after creating tables by seam, but i can't find any sql statement in the dvdstore example about creating tables, and how does the seam know the type of database fields?

        • 1. Re: when and how does the seam create  tables in the databas
          Samuel Mendenhall Apprentice

          Hibernate is going to automatically create the tables based on which POJOs are declared as @Entity beans and the subsequent annotations to the getter methods in that Entity bean. Hibernate will do this as long as your enforce a create-drop policy, or update I believe.

          Entity beans must have an id field denoted by @Id and typically @GeneratedValue, other than that, every other field is created as a column mapped by the Type of the field, unless otherwise specified.

          If you already have the database structure set up in the database, you could also mirror the structure in your Entity beans (given it the db structure isn't too difficult, or you're really good in sql).

          • 2. Re: when and how does the seam create  tables in the databas
            Todd Main Newbie

            I believe it is the following property (in bold) in the peristence.xml file (from the booking example):

             <persistence-unit name="bookingDatabase">
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
             <property name="hibernate.show_sql" value="true"/>
             <!-- These are the default for JBoss EJB3, but not for HEM: -->
             <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
             <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>

            This actually bit me on butt when I first incorporated the seam setup into my existing project, as it dropped my tables and recreated them. Ouch - so mych for my test data! I learned that lesson pretty quickly.

            • 4. Re: when and how does the seam create  tables in the databas
              hdslkf hklsdg Apprentice

              In a Seam project (or maybe for kinds of EJB projects using Hibernate?) the database is created during server start (plus import.sql gets imported). This is an important difference to e.g. AppFuse (Spring + Hibernate), where the database gets created by an ant task which you have to call explicetly (btw. this is the better solution in my opinion... but you can argue about that *g*).

              The line texan made bold is an important one. There are two values (as far as I know), and I experienced the following behaviour:

              create-drop: The database is recreated every time your server starts. If there is already a database it gets dropped before, so all your data entries vanish.

              update: The database is only created once, and your data "survives" server restarts. But if you change your entities by adding new fields the new fields get created in the database, too (upon server restart, remember ;)). If you delete entity bean fields the fields in the database remain. And if you change fields (e.g. from type Long to String, or from @ManyToOne to @ManyToMany) you will run into trouble, so you better do not do this ;).

              • 5. Re: when and how does the seam create  tables in the databas
                Gavin King Master

                Hibernate also supports the Ant-task approach.