Hibernate drop and create table

Viewed 141

Is some way to drop and create table on start of hibernate session? But only one from few mapped tables.

My hibernate configuration:

<hibernate-configuration>

<session-factory>

    <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/smart_home</property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>
    <property name="connection.pool_size">1</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="current_session_context_class">thread</property>
    <property name="show_sql">false</property>
    <property name="hbm2ddl.auto">update</property>

    <mapping class="pl.smart.home.database.User" />
    <mapping class="pl.smart.home.database.UnrecognisedDevice" />
    <mapping class="pl.smart.home.database.Sensor" />
    <mapping class="pl.smart.home.database.TypeDevice" />
    <mapping class="pl.smart.home.database.Event" />
    <mapping class="pl.smart.home.database.RecognisedDevice" />
    <mapping class="pl.smart.home.database.Status" />

</session-factory>

I want drop and create "event" table.

1 Answers

You can use create-drop

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

Drop the schema and recreate it on SessionFactory startup. Additionally, drop the schema on SessionFactory shutdown.

Related