如果 hsqlbd 处于服务器模式,则无法在初始化数据库上创建架构
我有 Hsqldb 1) 在内存中
<jdbc:embedded-database id="idtSimulatorDataSource" type="HSQL">
<jdbc:script location="classpath:hsql/idt_schema.sql"/>
</jdbc:embedded-database>
2) 在服务器模式
<bean id="idtSimulatorDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
<property name="url" value="jdbc:hsqldb:hsql://localhost:9001/idt_simulatordb"/>
</bean>
1) 一切正常 2)无法创建架构。如果我手动创建架构,那么 hibernate 将创建所有必要的 DDL 操作,并且everethyng 也可以正常工作。 但我不知道应该在哪里为数据库初始化脚本分配属性
这是我的持久性文件
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="IDTSimulator" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.nxsystems.simulator.idt.domain.IDTAccount</class>
<class>com.nxsystems.simulator.idt.domain.IDTCard</class>
<class>com.nxsystems.simulator.idt.domain.IDTCurrency</class>
<class>com.nxsystems.simulator.idt.domain.IDTCardHolder</class>
<class>com.nxsystems.simulator.idt.domain.IDTProgram</class>
<class>com.nxsystems.simulator.idt.domain.IDTTransaction</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- -->
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.default_schema" value="simulators"/>
<!-- -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<!-- -->
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="false"/>
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
<!-- -->
<!--<property name="hibernate.hbm2ddl.auto" value="create"/>-->
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.connection.pool_size" value="10"/>
</properties>
</persistence-unit>
</persistence>
这是我的初始化文件脚本
DROP SCHEMA IF EXISTS simulators CASCADE;
CREATE SCHEMA SIMULATORS AUTHORIZATION SA -- not work yet
CREATE TABLE SIMULATORS.dirtyhack();
也许自动创建模式会更好,但我不知道如何
更新 我已经找到如何启动 db init 脚本:
jdbc:initialize-database data-source="idtSimulatorDataSource">
我还设置了
<property name="hibernate.hbm2ddl.auto" value="create"/>
但现在出现以下错误:
11:15:57,337 WARN (main) [JDBCExceptionReporter] SQL Error: -5501, SQLState: 42501
11:15:57,370 ERROR (main) [JDBCExceptionReporter] user lacks privilege or object not found: IDT_CARD
http://publib.boulder.ibm.com/iseries/v5r1/ic2924/index.htm?info/rzala/rzalastc.html
SQLSTATE Value: 42501
Meaning: The authorization ID does not have the privilege to perform the specified operation on the identified object.
什么是权限?
更新: 现在我真的很困惑。 我查看日志。并参见以下内容:
/C2/SET SCHEMA PUBLIC DROP SCHEMA IF EXISTS 模拟器 CASCADE 创建模式模拟器授权 SA 创建表 SIMULATORS.dirtyhack() DISCONNECT /C3/SET SCHEMA PUBLIC DISCONNECT /C4/SET SCHEMA PUBLIC DROP SCHEMA IF EXISTS 模拟器 CASCADE 创建架构模拟器授权 SA 断开 /C5/SET SCHEMA 公共断开连接 /C6/SET SCHEMA PUBLIC
但我最近更改了我的初始化脚本:
DROP SCHEMA IF EXISTS simulators CASCADE;
CREATE SCHEMA SIMULATORS AUTHORIZATION sa -- not work yet
CREATE TABLE SIMULATORS.dirtyhack1();
我的 dirtyhack1 () 表在哪里??? 我删除了所有关于以前发布的内容
更新
我找到了原因。 该块在 Hibernate 创建其 DDL 操作后执行:
<jdbc:embedded-database id="idtSimulatorDataSource" type="HSQL">
<jdbc:script location="classpath:hsql/idt_schema.sql"/>
</jdbc:embedded-database>
我的代码包含这样的行:
public static Server server = new Server();
@AfterClass
public static void tearDown1() throws Exception {
server.shutdown();
}
@BeforeClass
public static void setUp() throws Exception {
HsqlProperties p = new HsqlProperties();
p.setProperty("server.database.0", "file:./idt_simulatordb");
p.setProperty("server.dbname.0", "idt_simulatordb");
p.setProperty("server.port", "9001");
server.setProperties(p);
server.start();
Connection conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost:9001/idt_simulatordb", "sa", "");
conn.setAutoCommit(true);
Statement st = conn.createStatement(); // statements
String expression1 = "DROP SCHEMA IF EXISTS simulators CASCADE;\n";
(1)String expression2 = "CREATE SCHEMA SIMULATORS AUTHORIZATION SA;";
String expression3 = "CREATE TABLE SIMULATORS.dirtyhack1();";
st.executeUpdate(expression1); // run the query
st.executeUpdate(expression2); // run the query
st.executeUpdate(expression3); // run the query
st.close();
conn.close();
}
这就是为什么我没有找到我的 dirtyhack1 表,这就是为什么我收到错误。初始化数据库刚刚删除了我的模拟器架构
所以有一个问题。如何在 Hibernate 之前启动 init 脚本?因为如果我不写第 (1) 行,那么我会收到有关错误架构导出的错误
I have Hsqldb 1) in memory
<jdbc:embedded-database id="idtSimulatorDataSource" type="HSQL">
<jdbc:script location="classpath:hsql/idt_schema.sql"/>
</jdbc:embedded-database>
2) on server-mode
<bean id="idtSimulatorDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
<property name="url" value="jdbc:hsqldb:hsql://localhost:9001/idt_simulatordb"/>
</bean>
1) all works properly
2) can't create schema. If I create schema manually, then hibernate create all necessary DDL manipulations and everethyng works properly too.
But I don't know where should I assign property for db initialization script
Here are my persistence file
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="IDTSimulator" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.nxsystems.simulator.idt.domain.IDTAccount</class>
<class>com.nxsystems.simulator.idt.domain.IDTCard</class>
<class>com.nxsystems.simulator.idt.domain.IDTCurrency</class>
<class>com.nxsystems.simulator.idt.domain.IDTCardHolder</class>
<class>com.nxsystems.simulator.idt.domain.IDTProgram</class>
<class>com.nxsystems.simulator.idt.domain.IDTTransaction</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- -->
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.default_schema" value="simulators"/>
<!-- -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<!-- -->
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="false"/>
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
<!-- -->
<!--<property name="hibernate.hbm2ddl.auto" value="create"/>-->
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.connection.pool_size" value="10"/>
</properties>
</persistence-unit>
</persistence>
And here are my init file script
DROP SCHEMA IF EXISTS simulators CASCADE;
CREATE SCHEMA SIMULATORS AUTHORIZATION SA -- not work yet
CREATE TABLE SIMULATORS.dirtyhack();
Maybe it would be better to create schema automatically, but I don't know how
UPDATE
I've founf how to launch db init script:
jdbc:initialize-database data-source="idtSimulatorDataSource">
Also I set
<property name="hibernate.hbm2ddl.auto" value="create"/>
But now I've got following error:
11:15:57,337 WARN (main) [JDBCExceptionReporter] SQL Error: -5501, SQLState: 42501
11:15:57,370 ERROR (main) [JDBCExceptionReporter] user lacks privilege or object not found: IDT_CARD
http://publib.boulder.ibm.com/iseries/v5r1/ic2924/index.htm?info/rzala/rzalastc.html
SQLSTATE Value: 42501
Meaning: The authorization ID does not have the privilege to perform the specified operation on the identified object.
What are privileges ?????
UPDATE:
Now I'm really confused.
I look into log. And see following:
/C2/SET SCHEMA PUBLIC DROP SCHEMA IF EXISTS simulators CASCADE
CREATE SCHEMA SIMULATORS AUTHORIZATION SA CREATE TABLE
SIMULATORS.dirtyhack() DISCONNECT /C3/SET SCHEMA PUBLIC DISCONNECT
/C4/SET SCHEMA PUBLIC DROP SCHEMA IF EXISTS simulators CASCADE
CREATE SCHEMA SIMULATORS AUTHORIZATION SA DISCONNECT /C5/SET SCHEMA
PUBLIC DISCONNECT /C6/SET SCHEMA PUBLIC
But I recently changed my init script:
DROP SCHEMA IF EXISTS simulators CASCADE;
CREATE SCHEMA SIMULATORS AUTHORIZATION sa -- not work yet
CREATE TABLE SIMULATORS.dirtyhack1();
Where is my dirtyhack1 () table???
I deleted all mentions about previous launches
UPDATE
I've found the reason.
This block is executed after Hibernate create its DDL manipulations:
<jdbc:embedded-database id="idtSimulatorDataSource" type="HSQL">
<jdbc:script location="classpath:hsql/idt_schema.sql"/>
</jdbc:embedded-database>
And my code contains such lines:
public static Server server = new Server();
@AfterClass
public static void tearDown1() throws Exception {
server.shutdown();
}
@BeforeClass
public static void setUp() throws Exception {
HsqlProperties p = new HsqlProperties();
p.setProperty("server.database.0", "file:./idt_simulatordb");
p.setProperty("server.dbname.0", "idt_simulatordb");
p.setProperty("server.port", "9001");
server.setProperties(p);
server.start();
Connection conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost:9001/idt_simulatordb", "sa", "");
conn.setAutoCommit(true);
Statement st = conn.createStatement(); // statements
String expression1 = "DROP SCHEMA IF EXISTS simulators CASCADE;\n";
(1)String expression2 = "CREATE SCHEMA SIMULATORS AUTHORIZATION SA;";
String expression3 = "CREATE TABLE SIMULATORS.dirtyhack1();";
st.executeUpdate(expression1); // run the query
st.executeUpdate(expression2); // run the query
st.executeUpdate(expression3); // run the query
st.close();
conn.close();
}
That's why I didn't find my dirtyhack1 table, and that's why I got error. Init-database just removed my simulators schema
So there is a question. How to launch init script before Hibernate? Cause if I wouldn't write line (1) then I get error about wrong schema export
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Spring EmbeddedDatabaseBuilder 创建数据库。像这样的事情:
那么您不需要 Hibernate 为您创建架构,因为它已经存在。
You could use the Spring EmbeddedDatabaseBuilder to create the database. Something like this:
Then you don't need Hibernate to create the schema for you because it will already exist.