2010-05-20 23 views
2

我使用的是spring,我所有帶註釋的實體類信息都放在ApplicationContext.xml中。我正在使用MySql數據庫,現在如何在休眠中使用SchemaExport函數來創建表?我的應用程序無法自動創建表格,但我已設置<prop key="hbm2ddl.auto">create</prop>。這是我的applicationContext.xml:我使用spring,hibernate和mysql。我如何讓我的應用程序自動創建表格

<?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:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql:///edde" /> 
    <property name="username" value="root" /> 
    <property name="password" value="" /> 
</bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="annotatedClasses"> 
    <list> 
    <value>com.edde.Book</value> 
    </list> 
    </property> 
    <property name="hibernateProperties"> 
    <props> 
    <prop key="current_session_context_class">thread</prop> 
    <prop key="show_sql">true</prop> 
    <prop key="hbm2ddl.auto">create</prop> 
    <prop key="hibernate.show_sql">true</prop> 
    <prop key="hibernate.use_sql_comments">true</prop> 
    <prop key="hibernate.format_sql">true</prop> 
    </props> 
    </property> 
</bean> 

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

<tx:annotation-driven transaction-manager="transactionManager" /> 

<bean id="bookDao" class="com.edde.BookDao"> 
    <property name="sessionFactory" ref="sessionFactory"></property> 
</bean> 

<bean id="bookService" class="com.edde.BookServiceImpl"> 
    <property name="dao" ref="bookDao"></property> 
</bean> 

</beans> 

這是我的測試應用程序:

package com.edde; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class Test { 

/** 
    * @param args 
    */ 
public static void main(String[] args) { 
    ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    BookService service = (BookService) factory.getBean("bookService"); 

    Book book = new Book(); 
    book.setBookName("great book"); 

    service.saveBook(book); 

} 

} 

當我運行我的應用程序,我得到這個錯誤:

2010-05-20 17:25:20,777 DEBUG [org.hibernate.SQL] - 
    /* insert com.edde.Book 
     */ insert 
     into 
      Book 
      (author, bookName, publication) 
     values 
      (?, ?, ?) 
Hibernate: 
    /* insert com.edde.Book 
     */ insert 
     into 
      Book 
      (author, bookName, publication) 
     values 
      (?, ?, ?) 
2010-05-20 17:25:20,809 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 
2010-05-20 17:25:20,809 DEBUG [org.hibernate.util.JDBCExceptionReporter] - could not insert: [com.edde.Book] [/* insert com.edde.Book */ insert into Book (author, bookName, publication) values (?, ?, ?)] 
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'edde.book' doesn't exist 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) 
at com.mysql.jdbc.Util.getInstance(Util.java:384) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696) 
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2398) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2316) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2301) 
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94) 
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) 
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329) 
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836) 
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) 
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268) 
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321) 
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) 
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130) 
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) 
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) 
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) 
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) 
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) 
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705) 
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693) 
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689) 
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:686) 
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:1) 
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406) 
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) 
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683) 
at com.edde.BookDao.insertBook(BookDao.java:18) 
at com.edde.BookServiceImpl.saveBook(BookServiceImpl.java:34) 
at com.edde.Test.main(Test.java:17) 
2010-05-20 17:25:20,809 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 1146, SQLState: 42S02 
2010-05-20 17:25:20,809 ERROR [org.hibernate.util.JDBCExceptionReporter] - Table 'edde.book' doesn't exist 
2010-05-20 17:25:20,809 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session 
2010-05-20 17:25:20,809 DEBUG [org.hibernate.jdbc.ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] 
2010-05-20 17:25:20,809 DEBUG [org.hibernate.jdbc.ConnectionManager] - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources! 
Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [com.edde.Book]; SQL [/* insert com.edde.Book */ insert into Book (author, bookName, publication) values (?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.edde.Book] 
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:629) 
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) 
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) 
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) 
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683) 
at com.edde.BookDao.insertBook(BookDao.java:18) 
at com.edde.BookServiceImpl.saveBook(BookServiceImpl.java:34) 
at com.edde.Test.main(Test.java:17) 
Caused by: org.hibernate.exception.SQLGrammarException: could not insert: [com.edde.Book] 
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92) 
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64) 
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329) 
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836) 
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) 
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268) 
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321) 
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) 
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130) 
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) 
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) 
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) 
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) 
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) 
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705) 
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693) 
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689) 
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:686) 
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:1) 
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406) 
... 5 more 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'edde.book' doesn't exist 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) 
at com.mysql.jdbc.Util.getInstance(Util.java:384) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696) 
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2398) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2316) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2301) 
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94) 
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) 
... 23 more 

現在我的問題是: 如何自動創建表格?

謝謝。

回答

5

使用hibernate.hbm2ddl.auto而不是hbm2ddl.auto

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

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

嗨布魯諾。 我已經將hbm2ddl設置爲**創建** – 2010-05-20 09:38:36

+1

@Yousui,您是否在屬性名稱中添加了'hibernate.'前綴? – 2010-05-20 09:41:44

+0

上帝,我錯過了**休眠**部分!非常感謝布魯諾! – 2010-05-20 09:42:26

相關問題