hibernate auto创建内存中的hsqldb会导致找不到Sequence

时间:2010-10-12 14:31:41

标签: java hibernate jpa hsqldb openejb

我正在使用hibernate作为我的jpa提供程序,并希望它创建一个 使用以下内容启动内存中的hsqldb: hibernate.hbm2ddl.auto =创建

但由于某种原因,我在日志中遇到如下例外情况。 事情似乎不然。这是一个hibernate还是hsqldb问题?

我只能使用jpa 1所以我正在使用 hsqldb 1.8.0.10和hibernate 3.3.0.SP1

这类似于: Internal HSQL database complains about privileges

ERROR - 4. Statement.executeUpdate(drop sequence DDS_EMAIL_STATUS_SEQ) FAILED! drop sequence DDS_EMAIL_STATUS_SEQ {FAILED after 0 msec}
java.sql.SQLException: Sequence not found in statement [drop sequence DDS_EMAIL_STATUS_SEQ]
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.jdbcStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.jdbcStatement.executeUpdate(Unknown Source)
    at net.sf.log4jdbc.StatementSpy.executeUpdate(StatementSpy.java:694)
    at org.apache.commons.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
    at org.apache.commons.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
    at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:383)
    at org.hibernate.tool.hbm2ddl.SchemaExport.drop(SchemaExport.java:358)
    at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:258)
    at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:211)
    at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:343)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:132)
    at org.apache.openejb.assembler.classic.PersistenceBuilder.createEntityManagerFactory(PersistenceBuilder.java:184)
    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:490)
    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:450)
    at org.apache.openejb.assembler.classic.Assembler.buildContainerSystem(Assembler.java:368)
    at org.apache.openejb.assembler.classic.Assembler.build(Assembler.java:280)
    at org.apache.openejb.OpenEJB$Instance.(OpenEJB.java:125)
    at org.apache.openejb.OpenEJB$Instance.(OpenEJB.java:60)
    at org.apache.openejb.OpenEJB.init(OpenEJB.java:271)
    at org.apache.openejb.OpenEJB.init(OpenEJB.java:250)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.openejb.loader.OpenEJBInstance.init(OpenEJBInstance.java:36)
    at org.apache.openejb.client.LocalInitialContextFactory.init(LocalInitialContextFactory.java:71)
    at org.apache.openejb.client.LocalInitialContextFactory.init(LocalInitialContextFactory.java:53)
    at org.apache.openejb.client.LocalInitialContextFactory.getInitialContext(LocalInitialContextFactory.java:42)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
    at javax.naming.InitialContext.init(InitialContext.java:223)
    at javax.naming.InitialContext.(InitialContext.java:197)

2 个答案:

答案 0 :(得分:3)

由于您正在使用内存数据库,因此在导出时不会出现与任何其他数据库对象一样的序列也就不足为奇了。但我不知道为什么你会得到这样的完整堆栈跟踪。我刚尝试了一个基本的测试用例,这就是我得到的:

16:27:07.708 [main] DEBUG o.h.tool.hbm2ddl.SchemaExport - Unsuccessful: drop sequence MY_ENTITY_SEQ
16:27:07.709 [main] DEBUG o.h.tool.hbm2ddl.SchemaExport - Sequence not found in statement [drop sequence MY_ENTITY_SEQ]

换句话说,它“默默地”失败。

也许您可以尝试扩展HSQLDialect并覆盖以下方法:

protected String getDropSequenceString(String sequenceName) {
    return "drop sequence " + sequenceName;
}

进入:

protected String getDropSequenceString(String sequenceName) {
    return "drop sequence " + sequenceName + " if exists";
}

虽未经过测试。

答案 1 :(得分:1)

这是一种我喜欢用来在每次测试中获得新的内存数据库的技术。

@Override
public void setUp() throws Exception {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
    p.put("movieDatabase", "new://Resource?type=DataSource");
    p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
    p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb" + System.currentTimeMillis());
    p.put("openejb.embedded.initialcontext.close", "DESTROY");

    initialContext = new InitialContext(p);
}

@Override
protected void tearDown() throws Exception {
    initialContext.close();
}

基本上,通过附加System.currentTimeMillis()来提供唯一的数据库名称,并为每个测试重建嵌入式容器。

让您不必在测试后进行清理,但每次测试都不需要新的jvm。