2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.hibernate
.unit
;
19 import java
.sql
.Connection
;
20 import java
.util
.List
;
21 import java
.util
.Properties
;
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
;
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.
33 public class DbModelHibernate
implements DbModel
{
34 private String dialect
;
35 private List
<String
> mappings
;
37 /** Sets the Hibernate dialect to use. */
38 public void setDialect(String dialect
) {
39 this.dialect
= dialect
;
42 /** Sets the list of mappings to consider. */
43 public void setMappings(List
<String
> mappings
) {
44 this.mappings
= mappings
;
48 * Creates an Hibernate schema export tool, in order to create the
49 * underlying datamodel.
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
);
58 for (String mapping
: mappings
) {
59 configuration
.addResource(mapping
.trim());
62 return new SchemaExport(configuration
, connection
);
65 public void createSchema(Connection connection
) {
66 SchemaExport schemaExport
= createSchemaExport(connection
);
67 schemaExport
.create(true, true);