2012-07-02 16 views
9

Mam projekt Maven ze strukturą Hibernate i Spring. Chcę, aby Hibernate automatycznie tworzył tabele, ale wszystkie istniejące tabele zostały po prostu usunięte, a wymagane tabele nie zostały utworzone. Żadnych wyjątków są wyrzucane podczas inicjalizacji sesji fabryki, ale gdy próbuję zapisać podmiot Player, jest wyjątek:Hibernacja nie tworzy tabel automatycznie

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: stół „billboarddb.player” nie istnieje

Gdybym tworzyć tabele ręcznie i zmień właściwość hibernate.hbm2ddl.auto do "validate", następnie wszystko działa poprawnie. Czy masz pojęcie, dlaczego Hibernacja nie tworzy tabel?

Wiosna plik konfiguracyjny: 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:

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 Zależności:

<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> 
+0

Duplikat http://stackoverflow.com/questions/4507142/does-hibernate-create-tables-in-the-database-automatically – JMelnik

+0

Tak, są podobne, ale tabele są generowane dobrze. – Balconsky

+0

Po prostu dać ci pomysł. Jeśli wszystko jest poprawnie skonfigurowane Sprawdź ustawienia dialektu dla swojej bazy –

Odpowiedz

25

Rozwiązuję problem. Hibernate nie może tworzyć tabel z MysqlInnoDBDialect. Teraz używam MySQL5InnoDBDialect zamiast.

+1

Ten fatalweb link już nie działa dla mnie: '( – iamanyone

+1

Użyłem wcześniej 'MySQLDialect' i zmieniłem go do 'MySQL5Dialect' (dodano" 5 ") .Zrobiłem też pracę. – Froxx

4

Dodaj następujący wpis do hibernateProperties rekwizytów.

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

Zdefiniowałem to: " $ {hibernate.hbm2ddl.auto}" – Balconsky

+0

y to jest rzucanie "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException"? czy mapowania są poprawne? –

+0

Tak, mapowanie jest w porządku. – Balconsky

Powiązane problemy