43

Upon reading this (archived) tutorial, they have not mentioned anything over creating tables in the DB. Does the Hibernate handle it automatically by creating tables and fields once i specify them.

Here is my beans configuration.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
    
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring"/>
        <property name="username" value="monwwty"/>
        <property name="password" value="www"/>
    </bean>
    
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>uk.co.vinoth.spring.domain.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>
    
    <bean id="myUserDAO" class="uk.co.vinoth.spring.dao.UserDAOImpl">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>
    
    <bean name="/user/*.htm" class="uk.co.vinoth.spring.web.UserController" >
        <property name="userDAO" ref="myUserDAO" />
    </bean>
    
</beans>
drac_o
  • 427
  • 5
  • 11
theJava
  • 14,620
  • 45
  • 131
  • 172

8 Answers8

56

your hibernate.hbm2ddl.auto setting should be defining that the database is created (options are validate, create, update or create-drop)

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
David O'Meara
  • 2,983
  • 25
  • 38
23

Yes it does in your case because of the below property in your config. This is ok during testing but in production you need to disable this.

<prop key="hibernate.hbm2ddl.auto">create</prop>
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • 3
    Could you elaborate on why disabling this during production is advisable? – William Morrison Jun 17 '13 at 20:52
  • 1
    "The built-in Hibernate connection pool is in no way intended for production use. It lacks several features found on production-ready connection pools." http://docs.jboss.org/hibernate/orm/4.2/quickstart/en-US/html/ch02.html – Kevin Meredith Jul 06 '13 at 17:29
6

For me it wasn't working even with hibernate.hbm2ddl.auto set to update. It turned out that the generated creation SQL was invalid, because one of my column names (user) was an SQL keyword. This failed softly, and it wasn't obvious what was going on until I inspected the logs.

z0r
  • 8,185
  • 4
  • 64
  • 83
6

add following property in your hibernate.cfg.xml file

<property name="hibernate.hbm2ddl.auto">update</property>

BTW, in your Entity class, you must define your @Id filed like this:

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "id")
private long id;

if you use the following definition, it maybe not work:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
Aaric Chen
  • 1,138
  • 11
  • 12
6

Yes, Hibernate can be configured by way of the hibernate.hbm2ddl.auto property in the hibernate.cfg.xml file to automatically create tables in your DB in order to store your entities in them if the table doesn't already exist.

This can be handy during development where a new, in-memory, DB can be used and created a new on every run of the application or during testing.

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
prem
  • 61
  • 1
  • 1
5

If property hibernate.ddl-auto = update, then it will not create the tables automatically. To create tables automatically, you need to set the property to hibernate.ddl-auto = create

The list of option which is used in the spring boot are

  • validate: validate the schema, makes no changes to the database.

  • update: update the schema.

  • create: creates the schema, destroying previous data.

  • create-drop: drop the schema at the end of the session

  • none: is all other cases

So for the first time you can set it to create and then next time on-wards you should set it to update.

KC_911723
  • 163
  • 2
  • 4
0

yes you can use

<property name="hbm2ddl.auto" value="create"/>
Ankit
  • 593
  • 4
  • 12
0

Hibernate can create a table, hibernate sequence and tables used for many-to-many mapping on your behalf but you have to explicitly configure it by calling setProperty("hibernate.hbm2ddl.auto", "create") of the Configuration object. By Default, it just validates the schema with DB and fails if anything not already exists by giving error "ORA-00942: table or view does not exist".

If you do above configuration then order of performed actions will be:- a) Drop all tables and sequence and do not give an error if they are not already present. b) create all table and sequence c) alter tables with constraints d) insert data into it.

Ashish Sharma
  • 574
  • 7
  • 18