]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/hibernate/unit/DbModelHibernate.java
Take into account OSGiBoot renaming
[gpl/argeo-slc.git] / runtime / org.argeo.slc.unit / src / main / java / org / argeo / slc / hibernate / unit / DbModelHibernate.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.hibernate.unit;
18
19 import java.sql.Connection;
20 import java.util.List;
21 import java.util.Properties;
22
23 import org.argeo.slc.support.deploy.db.DbModel;
24 import org.hibernate.cfg.Configuration;
25 import org.hibernate.cfg.Environment;
26 import org.hibernate.tool.hbm2ddl.SchemaExport;
27
28 /**
29 * Creates a relational data model from Hibernate mapping files. The benefit of
30 * this class is to be able to use Hibernate to have test data which are
31 * independent from the type of database used.
32 */
33 public class DbModelHibernate implements DbModel {
34 private String dialect;
35 private List<String> mappings;
36
37 /** Sets the Hibernate dialect to use. */
38 public void setDialect(String dialect) {
39 this.dialect = dialect;
40 }
41
42 /** Sets the list of mappings to consider. */
43 public void setMappings(List<String> mappings) {
44 this.mappings = mappings;
45 }
46
47 /**
48 * Creates an Hibernate schema export tool, in order to create the
49 * underlying datamodel.
50 */
51 protected SchemaExport createSchemaExport(Connection connection) {
52 Configuration configuration = new Configuration();
53 Properties properties = new Properties();
54 properties.setProperty(Environment.DIALECT, dialect);
55 properties.setProperty(Environment.HBM2DDL_AUTO, "create");
56 configuration.setProperties(properties);
57
58 for (String mapping : mappings) {
59 configuration.addResource(mapping.trim());
60 }
61
62 return new SchemaExport(configuration, connection);
63 }
64
65 public void createSchema(Connection connection) {
66 SchemaExport schemaExport = createSchemaExport(connection);
67 schemaExport.create(true, true);
68 }
69
70 }