2

Hi i need to save the database schema in a file. Like Hibernate do in its hbm files (save table names, columns name and types and primary foreign keys) Is there a pattern to do it?

4
  • So you want to read all the database meatdata manually and store it in a file? Commented May 27, 2012 at 20:02
  • But with or without Hibernate? I say this because you can use HibernateTools that can perform exactly as the hibernate property hibernate.hbm2ddl.auto, and write the result in a file (with the creation of tables, cols, keys, constraints, etc.) Commented May 27, 2012 at 20:05
  • Could be use Hibernate. I need this metadata in a single file because another application will read it. Commented May 27, 2012 at 20:09
  • do you have some link example about this @richarbernal Commented May 27, 2012 at 20:10

2 Answers 2

1

You can use the Hibernate class org.hibernate.tool.hbm2ddl.SchemaExport

You can pass it your Hibernate configuration and then use the method execute() wich prints the schema.

This is a code example:

Configuration cfg = new Configuration();
cfg.addResource(mappingFile);  // mapping to your hibernate mapping xml
cfg.setProperties(props);  // hibernate configuration properties, like dialect, connection url, etc.

SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.setDelimiter(";");
schemaExport.setOutputFile("database-script.sql");
schemaExport.setFormat(true);
schemaExport.execute(false, false, false, true);
Sign up to request clarification or add additional context in comments.

Comments

0

DBlook will do that for you.

http://www-304.ibm.com/support/docview.wss?uid=swg21381009

http://db.apache.org/derby/javadoc/publishedapi/jdbc4/org/apache/derby/tools/dblook.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.