使用HSQL进行Hibernate - 在Tomcat上启动应用程序时未创建表

时间:2012-02-09 06:34:34

标签: java spring hibernate hsqldb

我在Spring MVC中有一个应用程序,它使用Hibernate作为HSQL数据库的ORM。问题是我希望在启动应用程序时删除并创建数据库表。通过阅读Hibernate文档,我看到需要设置以下Hibernate设置:

<beans:prop key="hibernate.hbm2dll.auto">drop-create</beans:prop>

我尝试过“更新”和“创建”,但结果是一样的:当应用程序启动并尝试查找我的第一个表时,它不能,因为它尚未创建。我正在为表格使用注释。这是我收到的错误消息:

Hibernate: select this_.ID as ID1_0_, this_.CITY as CITY1_0_, this_.TEAMNAME as TEAMNAME1_0_ from TEAM this_
WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: -5501, SQLState: 42501
ERROR: org.hibernate.util.JDBCExceptionReporter - user lacks privilege or object not found: TEAM

那么这里有什么问题?以下是一些背景信息的相关文件。感谢。

servlet的context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
    <!-- Enables the transactional annotations -->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Connection to Database 

    *****   hsqldb.lock_file=false *****

    -->
    <beans:bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <beans:property name="driverClassName" value="org.hsqldb.jdbcDriver" />
        <beans:property name="url" value="jdbc:hsqldb:G:/SpringProjects/MVCProj1/database;shutdown=true" />                                     
        <beans:property name="username" value="ryan" />
        <beans:property name="password" value="ryan" />
    </beans:bean>

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <!-- View Resolver -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <!-- MessageSource -->
    <beans:bean
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <beans:property name="basename" value="classpath:messages" />
        <beans:property name="defaultEncoding" value="UTF-8" />
    </beans:bean>

    <!-- Hibernate SessionFactory -->
    <!-- <beans:bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   this was the original working bean class 2/3/12-->

        <beans:bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <beans:property name="dataSource" ref="datasource"></beans:property>
        <beans:property name="configLocation" value="classpath:hibernate.cfg.xml"></beans:property>
        <beans:property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"></beans:property>
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.ryans.MVCproject1.Team</beans:value>
                <beans:value>com.ryans.MVCproject1.Player</beans:value>
            </beans:list> 

        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
                <beans:prop key="hibernate.hbm2dll.auto">update</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>



     <!-- Define a transaction Manager -->
    <beans:bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory"></beans:property>
    </beans:bean>

    <context:component-scan base-package="com.ryans.MVCproject1" />


</beans:beans>

的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <mapping class="com.ryans.MVCproject1.Player"/>
        <mapping class="com.ryans.MVCproject1.Team"/>
    </session-factory>

</hibernate-configuration> 

1 个答案:

答案 0 :(得分:2)

您正在使用hibernate.hbm2dll.auto。 DLL =动态链接库。

正确的属性是hibernate.hbm2ddl.auto。 DDL =数据定义语言。