2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.hibernate
.unit
;
18 import java
.sql
.Connection
;
19 import java
.util
.List
;
20 import java
.util
.Properties
;
22 import org
.argeo
.slc
.support
.deploy
.db
.DbModel
;
23 import org
.hibernate
.cfg
.Configuration
;
24 import org
.hibernate
.cfg
.Environment
;
25 import org
.hibernate
.tool
.hbm2ddl
.SchemaExport
;
28 * Creates a relational data model from Hibernate mapping files. The benefit of
29 * this class is to be able to use Hibernate to have test data which are
30 * independent from the type of database used.
32 public class DbModelHibernate
implements DbModel
{
33 private String dialect
;
34 private List
<String
> mappings
;
36 /** Sets the Hibernate dialect to use. */
37 public void setDialect(String dialect
) {
38 this.dialect
= dialect
;
41 /** Sets the list of mappings to consider. */
42 public void setMappings(List
<String
> mappings
) {
43 this.mappings
= mappings
;
47 * Creates an Hibernate schema export tool, in order to create the
48 * underlying datamodel.
50 protected SchemaExport
createSchemaExport(Connection connection
) {
51 Configuration configuration
= new Configuration();
52 Properties properties
= new Properties();
53 properties
.setProperty(Environment
.DIALECT
, dialect
);
54 properties
.setProperty(Environment
.HBM2DDL_AUTO
, "create");
55 configuration
.setProperties(properties
);
57 for (String mapping
: mappings
) {
58 configuration
.addResource(mapping
.trim());
61 return new SchemaExport(configuration
, connection
);
64 public void createSchema(Connection connection
) {
65 SchemaExport schemaExport
= createSchemaExport(connection
);
66 schemaExport
.create(true, true);