]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.hibernate/src/main/java/org/argeo/slc/hibernate/unit/DbModelHibernate.java
Introduce Hibernate lazy loading for SlcExecution and TestResultCollections
[gpl/argeo-slc.git] / org.argeo.slc.hibernate / src / main / java / org / argeo / slc / hibernate / unit / DbModelHibernate.java
1 package org.argeo.slc.hibernate.unit;
2
3 import java.sql.Connection;
4 import java.util.List;
5 import java.util.Properties;
6
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;
11
12 /**
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.
16 */
17 public class DbModelHibernate implements DbModel {
18 private String dialect;
19 private List<String> mappings;
20
21 /** Sets the Hibernate dialect to use. */
22 public void setDialect(String dialect) {
23 this.dialect = dialect;
24 }
25
26 /** Sets the list of mappings to consider. */
27 public void setMappings(List<String> mappings) {
28 this.mappings = mappings;
29 }
30
31 /**
32 * Creates an Hibernate schema export tool, in order to create the
33 * underlying datamodel.
34 */
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);
41
42 for (String mapping : mappings) {
43 configuration.addResource(mapping.trim());
44 }
45
46 return new SchemaExport(configuration, connection);
47 }
48
49 public void createSchema(Connection connection) {
50 SchemaExport schemaExport = createSchemaExport(connection);
51 schemaExport.create(true, true);
52 }
53
54 }