1 package org
.argeo
.slc
.hibernate
.unit
;
3 import java
.sql
.Connection
;
5 import java
.util
.Properties
;
7 import org
.argeo
.slc
.support
.deploy
.db
.DbModel
;
8 import org
.hibernate
.cfg
.Configuration
;
9 import org
.hibernate
.cfg
.Environment
;
10 import org
.hibernate
.tool
.hbm2ddl
.SchemaExport
;
13 * Creates a relational data model from Hibernate mapping files. The benefit of
14 * this class is to be able to use Hibernate to have test data which are
15 * independent from the type of database used.
17 public class DbModelHibernate
implements DbModel
{
18 private String dialect
;
19 private List
<String
> mappings
;
21 /** Sets the Hibernate dialect to use. */
22 public void setDialect(String dialect
) {
23 this.dialect
= dialect
;
26 /** Sets the list of mappings to consider. */
27 public void setMappings(List
<String
> mappings
) {
28 this.mappings
= mappings
;
32 * Creates an Hibernate schema export tool, in order to create the
33 * underlying datamodel.
35 protected SchemaExport
createSchemaExport(Connection connection
) {
36 Configuration configuration
= new Configuration();
37 Properties properties
= new Properties();
38 properties
.setProperty(Environment
.DIALECT
, dialect
);
39 properties
.setProperty(Environment
.HBM2DDL_AUTO
, "create");
40 configuration
.setProperties(properties
);
42 for (String mapping
: mappings
) {
43 configuration
.addResource(mapping
.trim());
46 return new SchemaExport(configuration
, connection
);
49 public void createSchema(Connection connection
) {
50 SchemaExport schemaExport
= createSchemaExport(connection
);
51 schemaExport
.create(true, true);