我在单元测试中使用Apache Derby 10.9.x和Hibernate Entitymanager 4.1.9.Final。 Derby数据库模式从JPA带注释的实体生成。只有一个persistence.xml配置文件。我想在单元测试期间/之前/之后转储生成的Derby数据库模式。什么是编程方式?
解决方案:
// should not be required because Hibernate already started Derby:
//Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
try (Connection conn = DriverManager.getConnection ("jdbc:derby:memory:unit-testing;")) {
String cat = null;
String schema = "ROOT";
DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTableTypes();
ResultSetUtils.dump(rs);
rs = md.getTables(cat, schema, null, new String[]{"TABLE"});
ResultSetUtils.dump(rs);
rs = md.getColumns(cat, schema, null, null);
ResultSetUtils.dump(rs);
}
公共类ResultSetUtils {
private static final Logger logger = Logger.getLogger(ResultSetUtils.class.getName());
private static final String COL_SEPARATOR = ";";
public static int getColForLabel(ResultSet rs, String labelname) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
if (labelname.equals(rsmd.getColumnLabel(i))) {
return i;
}
}
throw new SQLException("Invalid label name " + labelname);
}
public static void dump(ResultSet rs) throws SQLException {
// the order of the rows in a cursor
// are implementation dependent unless you use the SQL ORDER statement
ResultSetMetaData meta = rs.getMetaData();
int colmax = meta.getColumnCount();
int i;
Object o;
StringBuilder sb = new StringBuilder(512);
for (i = 0; i < colmax; ++i) {
if(i>0) {
sb.append(COL_SEPARATOR);
}
String s = meta.getColumnName(i + 1);
sb.append((s == null) ? "NULL" : s);
s = meta.getColumnTypeName(i + 1);
sb.append((s == null) ? "(NULL)" : "("+s+")");
}
logger.info(sb.toString());
// the result set is a cursor into the data. You can only
// point to one row at a time
// assume we are pointing to BEFORE the first row
// rs.next() points to next row and returns true
// or false if there is no next row, which breaks the loop
for (; rs.next();) {
sb = new StringBuilder(512);
for (i = 0; i < colmax; ++i) {
if(i>0) {
sb.append(COL_SEPARATOR);
}
o = rs.getObject(i + 1); // Is SQL the first column is indexed
sb.append((o == null) ? "NULL" : o.toString());
}
logger.info(sb.toString());
}
}
}
答案 0 :(得分:1)
访问模式的编程方法是使用DatabaseMetaData类:http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html
从getTables()方法开始,打印出返回的信息。
答案 1 :(得分:1)
的pom.xml:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbytools</artifactId>
<version>10.9.1.0</version>
</dependency>
代码:
new dblook(new String[]{"-d", "jdbc:derby:memory:unit-testing;", "-verbose"});
无法打印出帮助:
new dblook(new String[]{});
结果:
-- Timestamp: 2013-03-09 00:19:49.733
-- Source database is: memory
-- Connection URL is: jdbc:derby:memory:unit-testing;
-- appendLogs: false
-- ----------------------------------------------
-- DDL Statements for schemas
-- ----------------------------------------------
CREATE SCHEMA "ROOT";
-- ----------------------------------------------
-- DDL Statements for tables
-- ----------------------------------------------
CREATE TABLE "ROOT"."BLOG" ("DTYPE" VARCHAR(31) NOT NULL, "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "BODY" VARCHAR(255), "CREATEDAT" TIMESTAMP, "RAWDATA" BLOB(2147483647), "SUBJECT" VARCHAR(255), "SENDER" VARCHAR(255));
-- ----------------------------------------------
-- DDL Statements for keys
-- ----------------------------------------------
-- primary/unique
ALTER TABLE "ROOT"."BLOG" ADD CONSTRAINT "SQL130309001949220" PRIMARY KEY ("ID");
JDBC元数据操作似乎缺少索引,触发器等一些东西。