Is it good practice to let Hibernate automatically create tables using <property name = "hbm2ddl.auto"> create </property>?
2 answers
If you use it in a test environment (if possible, using a database in memory such as h2 - yes, it's just a jar file ), this might be a good idea (make sure you don't have production-related settings)
Advice from Hibernate In Action, which can also be applied,
We saw Hibernate users trying to use SchemaUpdate to automatically update a production database schema. This can quickly end in disaster and will not be allowed by your database administrator .
If you just want to generate the circuit , prefer to use SchemaExport (class behind the scenes hbm2ddl)
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration
.addAnnotatedClass(<TYPE_YOUR_CLASS>.class)
.setProperty(Environment.USER, <TYPE_YOUR_USER>)
.setProperty(Environment.PASS, <TYPE_YOUR_PASSWORD>)
.setProperty(Environment.URL, <TYPE_YOUR_URL>)
.setProperty(Environment.DIALECT, <TYPE_YOUR_DIALECT>)
.setProperty(Environment.DRIVER, <TYPE_YOUR_DRIVER>);
SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile("schema.sql");
schema.create(<DO_YOU_WANT_TO_PRINT_TO_THE_CONSOLE>, <DO_YOU_WANT_TO_EXPORT_THE_SCRIPT_TO_THE_DATABASE>);
You can also enable Java properties according to the target machine.
-Dprofile=development
or
-Dprofile=production
( Spring, Spring Hibernate)
+2