gpt4 book ai didi

spring - 内存数据库未创建但日志显示执行了DDL

转载 作者:行者123 更新时间:2023-12-02 08:49:15 25 4
gpt4 key购买 nike

我正在尝试设置 spring 环境来运行内存数据库,该数据库是根据提供的映射自动创建的。在执行过程中,我可以在日志中看到执行了 DDL 语句,但是当 hibernate 尝试将数据插入到创建的表中时,我收到“找不到表”异常。

可能出现什么问题?

使用 spring 3.1.1 和 Hibernate 4.1.1 以及 H2 版本 1.3.165。

日志看起来像(仅留下相关记录):

INFO at '25-04-2012 13:23:56.318' by thread 'main' from category 'org.hibernate.tool.hbm2ddl.SchemaExport':
HHH000227: Running hbm2ddl schema export
DEBUG at '25-04-2012 13:23:56.318' by thread 'main' from category 'org.hibernate.SQL':

drop table DUMMIES if exists
Hibernate:
drop table DUMMIES if exists
DEBUG at '25-04-2012 13:23:56.318' by thread 'main' from category 'org.hibernate.SQL':

create table DUMMIES (
id bigint generated by default as identity,
title varchar(255),
primary key (id),
unique (title)
)
Hibernate:
create table DUMMIES (
id bigint generated by default as identity,
title varchar(255),
primary key (id),
unique (title)
)
INFO at '25-04-2012 13:23:56.334' by thread 'main' from category 'org.hibernate.tool.hbm2ddl.SchemaExport':
HHH000230: Schema export complete
INFO at '25-04-2012 13:23:56.334' by thread 'main' from category 'org.springframework.context.support.ClassPathXmlApplicationContext':
Bean 'mySessionFactory' of type [class org.springframework.orm.hibernate4.LocalSessionFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
INFO at '25-04-2012 13:23:56.631' by thread 'main' from category 'org.springframework.orm.hibernate4.HibernateTransactionManager':
Using DataSource [org.springframework.jdbc.datasource.SimpleDriverDataSource@a86d12] of Hibernate SessionFactory for HibernateTransactionManager
WARN at '25-04-2012 13:23:56.865' by thread 'main' from category 'org.hibernate.engine.jdbc.spi.SqlExceptionHelper':
SQL Error: 42102, SQLState: 42S02
ERROR at '25-04-2012 13:23:56.865' by thread 'main' from category 'org.hibernate.engine.jdbc.spi.SqlExceptionHelper':
Table "DUMMIES" not found; SQL statement:
insert into DUMMIES (id, title) values (null, ?) [42102-165]

Beans.xml 看起来像(“mypackage”是完整包名称的替换):

<bean id="myDataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:" />
...
</bean>

<bean id="mySessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan">
<array>
<value>mypackage.entities</value>
</array>
</property>
</bean>

<bean id="myTransactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>

<aop:config>
<aop:pointcut id="daoMethods" expression="execution(* mypackage.*Dao.*(..))" />
<aop:advisor advice-ref="requiredTxAdvice" pointcut-ref="daoMethods" />
</aop:config>

<tx:advice id="requiredTxAdvice" transaction-manager="myTransactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>

class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="dummyDao" class="mypackage.DummyDaoImpl">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>

hibernate.properties 看起来像:

hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.id.new_generator_mappings=true
hibernate.hbm2ddl.auto=create-drop

(创建也不起作用)

最佳答案

当您将数据库 URL 指定为 jdbc:h2:mem: 时,H2 会为每个连接创建新数据库,因此每个 Hibernate session 都会看到自己的空数据库。

因此,您需要指定数据库名称,以便从不同的连接访问同一数据库。此外,您还需要防止数据库在没有事件连接时被关闭。生成的 URL 如下所示:jdbc:h2:mem:foo;DB_CLOSE_DELAY=-1

另请注意,Spring 提供了对嵌入式数据库的内置支持,请参阅 12.8 Embedded database support .

关于spring - 内存数据库未创建但日志显示执行了DDL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10314216/

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