14

I have Maven project with Hibernate and Spring framework. I want Hibernate to create tables automatically, but all existing tables are just dropped and the required tables are not created. No exceptions are thrown during session factory initialization, but when I try save a Player entity, exception is thrown:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'billboarddb.player' doesn't exist

If I create the tables manually and change the property hibernate.hbm2ddl.auto to "validate", then everything works fine. Do you have any idea, why Hibernate does not create the tables?

Spring configuration file:

<context:component-scan  base-package="org.meluk.billboard.business.controller" />
<tx:annotation-driven transaction-manager="txManager" />
<bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" >
        <list>
            <value>/WEB-INF/config/jdbc.properties</value>
        </list>
    </property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${hibernate.connection.driver_class}" />
    <property name="url" value="${hibernate.connection.url}" />
    <property name="username" value="${hibernate.connection.username}" />
    <property name="password" value="${hibernate.connection.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="/WEB-INF/hibernate.cfg.xml" />
    <property name="packagesToScan" value="org.meluk.billboard.jpa" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop>
            <prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
            <prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
            <prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
        </props>
    </property>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

jdbc.properties file:

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://127.0.0.1:3306/BillboardDB
hibernate.connection.username=root
hibernate.connection.password=1234
hibernate.default_schema=BillboardDB
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.hbm2ddl.auto=create
hibernate.show_sql=true
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50

Hibernate dependencies:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>${hibernateVersion}</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-c3p0</artifactId>
  <version>${hibernateVersion}</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-tools</artifactId>
  <version>${hibernateToolsVersion}</version>
</dependency>
6
  • Duplicate of stackoverflow.com/questions/4507142/… Commented Jul 2, 2012 at 10:37
  • Yes they are similar, but there tables are generated fine. Commented Jul 2, 2012 at 10:41
  • Just to give you an idea. If everything is properly configured Check your dialect settings for your database Commented Jul 2, 2012 at 10:43
  • I`ve checked it a lot of times. Commented Jul 2, 2012 at 10:45
  • How have you defined the enitity, Player? Can you put the code snippet. Commented Jul 2, 2012 at 12:29

2 Answers 2

30

I solve the problem. Hibernate could not create tables with MysqlInnoDBDialect. Now I use MySQL5InnoDBDialect instead.

Sign up to request clarification or add additional context in comments.

1 Comment

I used MySQLDialect before and changed it to MySQL5Dialect (added the '5'). Did the job, too.
5

Add the following entry to the hibernateProperties props.

<prop key="hibernate.hbm2ddl.auto">create</prop>

4 Comments

I defined it: "<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>"
y it is throwing "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException" ? is mappings are all correct ?
you have put show sql as true right ? take the scripts comming in the logs and run them directly using your db client. see if it has problems.
I do not see any sql for drop or create tables. There is only:"INFO: Hibernate: insert into Player (age, email, password) values (?, ?, ?)"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.