]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/unit/DbModel.java
Add elements and tags in tree test result
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / unit / DbModel.java
1 package org.argeo.slc.unit;
2
3 import java.sql.Connection;
4 import java.util.List;
5 import java.util.Properties;
6
7 import org.hibernate.cfg.Configuration;
8 import org.hibernate.cfg.Environment;
9 import org.hibernate.tool.hbm2ddl.SchemaExport;
10
11 /**
12 * Creates a relational data model from Hibernate mapping files. The benefit of
13 * this class is to be able to use Hibernate to have test data which are
14 * independent from the type of database used.
15 */
16 public class DbModel {
17 private String dialect;
18 private List<String> mappings;
19
20 /** Sets the Hibernate dialect to use. */
21 public void setDialect(String dialect) {
22 this.dialect = dialect;
23 }
24
25 /** Sets the list of mappings to consider. */
26 public void setMappings(List<String> mappings) {
27 this.mappings = mappings;
28 }
29
30 /**
31 * Creates an Hibernate schema export tool, in order to create the
32 * underlying datamodel.
33 */
34 public SchemaExport createSchemaExport(Connection connection) {
35 Configuration configuration = new Configuration();
36 Properties properties = new Properties();
37 properties.setProperty(Environment.DIALECT, dialect);
38 properties.setProperty(Environment.HBM2DDL_AUTO, "create");
39 configuration.setProperties(properties);
40
41 for (String mapping : mappings) {
42 configuration.addResource(mapping.trim());
43 }
44
45 return new SchemaExport(configuration, connection);
46 }
47 }