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?
-
So you want to read all the database meatdata manually and store it in a file?home– home2012-05-27 20:02:20 +00:00Commented 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.)richarbernal– richarbernal2012-05-27 20:05:38 +00:00Commented May 27, 2012 at 20:05
-
Could be use Hibernate. I need this metadata in a single file because another application will read it.walves– walves2012-05-27 20:09:20 +00:00Commented May 27, 2012 at 20:09
-
do you have some link example about this @richarbernalwalves– walves2012-05-27 20:10:22 +00:00Commented May 27, 2012 at 20:10
Add a comment
|
2 Answers
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);
Comments
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