gpt4 book ai didi

java - Hibernate with HSQL - 在 Tomcat 上启动应用程序时未创建表

转载 作者:行者123 更新时间:2023-12-02 07:58:55 25 4
gpt4 key购买 nike

我在 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>

最佳答案

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

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

关于java - Hibernate with HSQL - 在 Tomcat 上启动应用程序时未创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9206440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com