未创建内存数据库,但日志显示已执行DDL [英] In-memory database is not created but logs shows that DDL was executed

查看:67
本文介绍了未创建内存数据库,但日志显示已执行DDL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置spring环境以在基于提供的映射自动创建的内存数据库中运行.在执行期间,我可以在日志中看到已执行DDL语句,但是当hibernate尝试将数据插入到已创建的表中时,出现找不到表"异常.

I am trying to setup spring environment to run with in memory database which is automatically created based on provided mappings. During execution I can see in logs that DDL statements are executed, but when hibernate tries to insert data into created tables, I am getting 'Table not found' exception.

有什么问题吗?

使用spring 3.1.1和Hibernate 4.1.1和H2版本1.3.165.

Using spring 3.1.1 and Hibernate 4.1.1 and H2 version 1.3.165.

日志看起来像(仅保留了一些新记录):

Logs looks like (only revelant records left):

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"代替了完整的软件包名称):

Beans.xml looks like ('mypackage' is a replacement for full package name):

<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.properties looks like:

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会话都会看到自己的空数据库.

When you specify database URL as jdbc:h2:mem: H2 creates new database for each connection, thus each Hibernate session sees its own empty database.

因此,您需要指定数据库名称,才能从不同的连接访问同一数据库.另外,您还需要防止没有活动连接的数据库被关闭.生成的URL如下所示:jdbc:h2:mem:foo;DB_CLOSE_DELAY=-1.

So, you need to specify database name, to access the same database from different connections. Also you need to prevent the database from being closed when it has no active connections. The resulting URL looks like this: jdbc:h2:mem:foo;DB_CLOSE_DELAY=-1.

还请注意,Spring为嵌入式数据库提供了内置支持,请参见

Also note that Spring provides built-in support for embedded databases, see 12.8 Embedded database support.

这篇关于未创建内存数据库,但日志显示已执行DDL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆