Tapas Bose
Tapas Bose

Reputation: 29806

Hibernate custom schema creation

<prop key="hibernate.hbm2ddl.auto">create</prop> creates a new database schema and <prop key="hibernate.hbm2ddl.auto">update</prop> create if it is not exists and update existing database schema. If I want to check whether database schema exists or not and depending on that a database schema will be created, how can I achieve this. Currently the configuration of my applicationContext.xml is:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="annotatedClasses">
        <list>
            <value>info.ems.models.User</value>
            <value>info.ems.models.Role</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>        
</bean>
<bean id="dao" class="info.ems.hibernate.HibernateEMSDao" init-method="createSchema">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>  

And the HibernateEMSDao.java:

public class HibernateEMSDao implements EMSDao {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private HibernateTemplate hibernateTemplate;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }    

    public void saveUser(User user) {
        hibernateTemplate.saveOrUpdate(user);
    }

    public List<User> listUser() {
        return hibernateTemplate.find("from User");
    }

    public void createSchema() {
        logger.info("inserting default admin user into database");
        User admin = new User();
        admin.setUsername("admin");
        admin.setName("Admin");
        admin.setEmail("admin");
        admin.setPassword("21232f297a57a5a743894a0e4a801fc3");
        saveUser(admin);
        logger.info("Admin inserted into database");

        try {
            System.out.println(listUser().get(0).getId());
        } catch (Exception e) {
            logger.error("===================Error================");
        }
    }               
}

It is working. What configuration will help me to gain this? Something like:

Thanks and regards.

Upvotes: 2

Views: 2496

Answers (1)

iruediger
iruediger

Reputation: 953

You could disable the hibernate.hbm2ddl.auto option, check the conditions (probably using plain JDBC) and call (or don't) the create method of the SchemaExport class. This would be done in your application's initialization code (a ServletContextListener in case you are working with a web app).

An example on how to use the SchemaExport class:

AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(info.ems.models.User.class);
config.addAnnotatedClass(info.ems.models.Role.class);
config.configure();
new SchemaExport(config).create(true, true);

Upvotes: 1

Related Questions